Skip to content

Commit 6922252

Browse files
authored
Merge pull request #834 from Unity-Technologies/unity-master-path-remapping
Implement path remapping support (case 992909) This was previously stubbed out when rebasing onto mono master branch. case 992909 - Fix FileNotFoundException encountered when using packages
2 parents bb0c4f9 + 155ca60 commit 6922252

File tree

5 files changed

+160
-91
lines changed

5 files changed

+160
-91
lines changed

mono/metadata/image.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,8 +1360,9 @@ do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
13601360
MonoCLIImageInfo *iinfo;
13611361
MonoImage *image;
13621362
MonoFileMap *filed;
1363-
1364-
gboolean remapped = mono_unity_file_remap_path(&fname);
1363+
const char *fname_remap;
1364+
if (fname_remap = mono_unity_remap_path (fname))
1365+
fname = fname_remap;
13651366

13661367
if ((filed = mono_file_map_open (fname)) == NULL){
13671368
if (IS_PORTABILITY_SET) {
@@ -1375,8 +1376,7 @@ do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
13751376
if (filed == NULL) {
13761377
if (status)
13771378
*status = MONO_IMAGE_ERROR_ERRNO;
1378-
if (remapped)
1379-
g_free((void*)fname);
1379+
g_free((void*)fname_remap);
13801380
return NULL;
13811381
}
13821382
}
@@ -1396,8 +1396,7 @@ do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
13961396
g_free (image);
13971397
if (status)
13981398
*status = MONO_IMAGE_IMAGE_INVALID;
1399-
if (remapped)
1400-
g_free((void*)fname);
1399+
g_free((void*)fname_remap);
14011400
return NULL;
14021401
}
14031402
iinfo = g_new0 (MonoCLIImageInfo, 1);
@@ -1411,8 +1410,7 @@ do_mono_image_open (const char *fname, MonoImageOpenStatus *status,
14111410
image->core_clr_platform_code = mono_security_core_clr_determine_platform_image (image);
14121411

14131412
mono_file_map_close (filed);
1414-
if (remapped)
1415-
g_free((void*)fname);
1413+
g_free((void*)fname_remap);
14161414
return do_mono_image_load (image, status, care_about_cli, care_about_pecoff);
14171415
}
14181416

mono/metadata/loader.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,26 +1115,26 @@ static MonoDl*
11151115
cached_module_load (const char *name, int flags, char **err)
11161116
{
11171117
MonoDl *res;
1118+
const char *name_remap;
11181119

11191120
if (err)
11201121
*err = NULL;
1121-
gboolean remapped = mono_unity_file_remap_path(&name);
1122+
if (name_remap = mono_unity_remap_path (name))
1123+
name = name_remap;
11221124
global_loader_data_lock ();
11231125
if (!global_module_map)
11241126
global_module_map = g_hash_table_new (g_str_hash, g_str_equal);
11251127
res = (MonoDl *)g_hash_table_lookup (global_module_map, name);
11261128
if (res) {
11271129
global_loader_data_unlock ();
1128-
if (remapped)
1129-
g_free((void*)name);
1130+
g_free((void*)name_remap);
11301131
return res;
11311132
}
11321133
res = mono_dl_open (name, flags, err);
11331134
if (res)
11341135
g_hash_table_insert (global_module_map, g_strdup (name), res);
11351136
global_loader_data_unlock ();
1136-
if (remapped)
1137-
g_free((void*)name);
1137+
g_free((void*)name_remap);
11381138
return res;
11391139
}
11401140

mono/metadata/unity-utils.c

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,14 +1197,20 @@ size_t RemapPathFunction (const char* path, char* buffer, size_t buffer_len)
11971197
*/
11981198
static RemapPathFunction g_RemapPathFunc = NULL;
11991199

1200+
void
1201+
mono_unity_register_path_remapper (RemapPathFunction func)
1202+
{
1203+
g_RemapPathFunc = func;
1204+
}
1205+
12001206
/* calls remapper function if registered; allocates memory if remapping is available */
12011207
static inline size_t
12021208
call_remapper(const char* path, char** buf)
12031209
{
12041210
size_t len;
12051211

12061212
if (!g_RemapPathFunc)
1207-
return FALSE;
1213+
return 0;
12081214

12091215
*buf = NULL;
12101216
len = g_RemapPathFunc(path, *buf, 0);
@@ -1218,80 +1224,67 @@ call_remapper(const char* path, char** buf)
12181224
return len;
12191225
}
12201226

1221-
/* updates 'path' if remapping is available; returns TRUE if updated (path must be free()'d) */
1222-
gboolean
1223-
mono_unity_file_remap_path(const char** path)
1227+
MonoBoolean
1228+
ves_icall_System_IO_MonoIO_RemapPath (MonoString *path, MonoString **new_path)
12241229
{
1225-
size_t len;
1226-
char * buf;
1230+
MonoError error;
1231+
const gunichar2* path_remapped;
12271232

1228-
len = call_remapper(*path, &buf);
1229-
if (len == 0)
1233+
if (!g_RemapPathFunc)
1234+
return 0;
1235+
1236+
path_remapped = mono_unity_remap_path_utf16 (mono_string_chars (path));
1237+
1238+
if (!path_remapped)
12301239
return FALSE;
12311240

1232-
*path = buf;
1241+
mono_gc_wbarrier_generic_store (new_path, (MonoObject*)mono_string_from_utf16_checked (path_remapped, &error));
1242+
1243+
g_free (path_remapped);
1244+
1245+
mono_error_set_pending_exception (&error);
1246+
12331247
return TRUE;
12341248
}
12351249

1250+
const char*
1251+
mono_unity_remap_path (const char* path)
1252+
{
1253+
const char* path_remap = NULL;
1254+
call_remapper (path, &path_remap);
12361255

1256+
return path_remap;
1257+
}
12371258

1238-
/* sets 'new_path', and returns TRUE, if remapping is available */
1239-
static gboolean
1240-
remap_path (MonoString *path, MonoString** new_path)
1259+
const gunichar2*
1260+
mono_unity_remap_path_utf16 (const gunichar2* path)
12411261
{
1242-
MonoError error;
1243-
MonoString * str;
1262+
const gunichar2* path_remap = NULL;
12441263
char * utf8_path;
12451264
char * buf;
12461265
char * path_end;
12471266
size_t len;
12481267

1249-
*new_path = NULL;
1250-
12511268
if (!g_RemapPathFunc)
1252-
return FALSE;
1269+
return path_remap;
12531270

1254-
utf8_path = mono_string_to_utf8_ignore(path);
1255-
len = call_remapper(utf8_path, &buf);
1271+
utf8_path = g_utf16_to_utf8 (path, -1, NULL, NULL, NULL);
1272+
len = call_remapper (utf8_path, &buf);
12561273
if (len == 0)
12571274
{
1258-
g_free(utf8_path);
1259-
return FALSE;
1275+
g_free (utf8_path);
1276+
return path_remap;
12601277
}
12611278

1262-
path_end = memchr(buf, '\0', len);
1263-
len = path_end ? (size_t) (path_end - buf) : len;
1264-
str = mono_string_new_len_checked (mono_domain_get (), buf, (guint)len, &error);
1279+
path_end = memchr (buf, '\0', len);
1280+
len = path_end ? (size_t)(path_end - buf) : len;
12651281

1266-
g_free(utf8_path);
1267-
g_free (buf);
1282+
path_remap = g_utf8_to_utf16 (buf, len, NULL, NULL, NULL);
12681283

1269-
mono_gc_wbarrier_generic_store(new_path, (MonoObject*)str);
1270-
mono_error_set_pending_exception (&error);
1271-
1272-
return *new_path ? TRUE : FALSE;
1273-
}
1274-
1275-
/* returns remapped path, if remapping is available. otherwise returns original path */
1276-
const gunichar2 *
1277-
mono_unity_get_remapped_path (const gunichar2 *path)
1278-
{
1279-
// JON TODO:
1280-
return path;
1281-
//MonoString * new_path;
1282-
//return remap_path(path, &new_path) ? new_path : path;
1283-
}
1284-
1285-
MonoBoolean
1286-
ves_icall_System_IO_MonoIO_RemapPath (MonoString *path, MonoString **new_path)
1287-
{
1288-
return remap_path(path, new_path);
1289-
}
1284+
g_free (utf8_path);
1285+
g_free (buf);
12901286

1291-
void
1292-
mono_unity_register_path_remapper(RemapPathFunction func)
1293-
{
1294-
g_RemapPathFunc = func;
1287+
return path_remap;
12951288
}
12961289

12971290
MonoMethod*

mono/metadata/unity-utils.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,15 @@ MONO_API MonoClass* mono_custom_attrs_get_attrs (MonoCustomAttrInfo *ainfo, gpoi
172172

173173
typedef size_t (*RemapPathFunction)(const char* path, char* buffer, size_t buffer_len);
174174
MONO_API void mono_unity_register_path_remapper (RemapPathFunction func);
175-
gboolean
176-
mono_unity_file_remap_path(const char** path);
175+
176+
const char*
177+
mono_unity_remap_path (const char* path);
178+
179+
const gunichar2*
180+
mono_unity_remap_path_utf16 (const gunichar2* path);
181+
177182
MonoBoolean
178183
ves_icall_System_IO_MonoIO_RemapPath (MonoString *path, MonoString **new_path);
179-
const gunichar2 *
180-
mono_unity_get_remapped_path (const gunichar2 *path);
181184

182185
MonoMethod*
183186
mono_method_get_method_definition(MonoMethod *method);

0 commit comments

Comments
 (0)