Skip to content

Commit 8805933

Browse files
committed
Fix based on CR
1 parent c5b2d35 commit 8805933

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

CustomTools/RoslynRuntimeCompilation/ManageRuntimeCompilation.cs

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ private static object CompileAndLoad(JObject @params)
8484
try
8585
{
8686
string code = @params["code"]?.ToString();
87-
string assemblyName = @params["assembly_name"]?.ToString() ?? $"DynamicAssembly_{DateTime.Now.Ticks}";
87+
var assemblyToken = @params["assembly_name"];
88+
string assemblyName = assemblyToken == null || string.IsNullOrWhiteSpace(assemblyToken.ToString())
89+
? $"DynamicAssembly_{DateTime.Now.Ticks}"
90+
: assemblyToken.ToString().Trim();
8891
string attachTo = @params["attach_to"]?.ToString();
8992
bool loadImmediately = @params["load_immediately"]?.ToObject<bool>() ?? true;
9093

@@ -101,8 +104,21 @@ private static object CompileAndLoad(JObject @params)
101104

102105
// Create output directory
103106
Directory.CreateDirectory(DynamicAssembliesPath);
104-
string dllPath = Path.Combine(DynamicAssembliesPath, $"{assemblyName}.dll");
105-
107+
string basePath = Path.GetFullPath(DynamicAssembliesPath);
108+
Directory.CreateDirectory(basePath);
109+
string safeFileName = SanitizeAssemblyFileName(assemblyName);
110+
string dllPath = Path.GetFullPath(Path.Combine(basePath, $"{safeFileName}.dll"));
111+
112+
if (!dllPath.StartsWith(basePath, StringComparison.Ordinal))
113+
{
114+
return Response.Error("Assembly name must resolve inside the dynamic assemblies directory.");
115+
}
116+
117+
if (File.Exists(dllPath))
118+
{
119+
dllPath = Path.GetFullPath(Path.Combine(basePath, $"{safeFileName}_{DateTime.Now.Ticks}.dll"));
120+
}
121+
106122
// Parse code
107123
var syntaxTree = CSharpSyntaxTree.ParseText(code);
108124

@@ -121,7 +137,7 @@ private static object CompileAndLoad(JObject @params)
121137

122138
// Emit to file
123139
EmitResult emitResult;
124-
using (var stream = new FileStream(dllPath, FileMode.Create))
140+
using (var stream = new FileStream(dllPath, FileMode.Create, FileAccess.Write, FileShare.None))
125141
{
126142
emitResult = compilation.Emit(stream);
127143
}
@@ -227,7 +243,7 @@ private static object CompileAndLoad(JObject @params)
227243
}
228244
#endif
229245
}
230-
246+
231247
private static object ListLoadedAssemblies()
232248
{
233249
var assemblies = LoadedAssemblies.Values.Select(info => new
@@ -238,14 +254,21 @@ private static object ListLoadedAssemblies()
238254
type_count = info.TypeNames.Count,
239255
types = info.TypeNames
240256
}).ToList();
241-
257+
242258
return Response.Success($"Found {assemblies.Count} loaded dynamic assemblies", new
243259
{
244260
count = assemblies.Count,
245261
assemblies = assemblies
246262
});
247263
}
248264

265+
private static string SanitizeAssemblyFileName(string assemblyName)
266+
{
267+
var invalidChars = Path.GetInvalidFileNameChars();
268+
var sanitized = new string(assemblyName.Where(c => !invalidChars.Contains(c)).ToArray());
269+
return string.IsNullOrWhiteSpace(sanitized) ? $"DynamicAssembly_{DateTime.Now.Ticks}" : sanitized;
270+
}
271+
249272
private static object GetAssemblyTypes(JObject @params)
250273
{
251274
string assemblyName = @params["assembly_name"]?.ToString();

CustomTools/RoslynRuntimeCompilation/RoslynRuntimeCompiler.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ public class CompilationHistoryEntry
9393

9494
// Static shared history
9595
private static System.Collections.Generic.List<CompilationHistoryEntry> _sharedHistory = new System.Collections.Generic.List<CompilationHistoryEntry>();
96-
private static int _maxHistoryEntries = 50;
9796

9897
public System.Collections.Generic.List<CompilationHistoryEntry> CompilationHistory => _sharedHistory;
9998

@@ -584,7 +583,7 @@ private void AddHistoryEntry(string sourceCode, string typeName, string methodNa
584583
_sharedHistory.Add(entry);
585584

586585
// Trim if exceeded max
587-
while (_sharedHistory.Count > _maxHistoryEntries)
586+
while (_sharedHistory.Count > maxHistoryEntries)
588587
{
589588
_sharedHistory.RemoveAt(0);
590589
}
@@ -1167,7 +1166,7 @@ void DrawHistoryTab()
11671166
if (helperInScene.SaveHistoryEntryAsScript(selectedHistoryIndex, out string path, out string error))
11681167
{
11691168
EditorUtility.DisplayDialog("Success", $"Script saved to:\n{path}", "OK");
1170-
System.Diagnostics.Process.Start("explorer.exe", $"/select,\"{path.Replace("/", "\\")}\"");
1169+
EditorUtility.RevealInFinder(path);
11711170
}
11721171
else
11731172
{

CustomTools/RoslynRuntimeCompilation/runtime_compilation_tool.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ async def compile_runtime_code(
8383
}
8484
```
8585
"""
86-
#await safe_info(ctx, f"Compiling runtime code for assembly: {assembly_name or 'auto-generated'}")
87-
await ctx.info(f"Compiling runtime code for assembly: {assembly_name or 'auto-generated'}")
86+
await safe_info(ctx, f"Compiling runtime code for assembly: {assembly_name or 'auto-generated'}")
8887

8988
params = {
9089
"action": "compile_and_load",
@@ -113,8 +112,7 @@ async def list_loaded_assemblies(
113112
- Load timestamps
114113
- DLL file paths
115114
"""
116-
#await safe_info(ctx, "Retrieving loaded dynamic assemblies...")
117-
await ctx.info("Retrieving loaded dynamic assemblies...")
115+
await safe_info(ctx, "Retrieving loaded dynamic assemblies...")
118116

119117
params = {"action": "list_loaded"}
120118
return handle_unity_command("runtime_compilation", params)
@@ -135,8 +133,7 @@ async def get_assembly_types(
135133
- Finding MonoBehaviour classes to attach
136134
- Debugging compilation results
137135
"""
138-
#await safe_info(ctx, f"Getting types from assembly: {assembly_name}")
139-
await ctx.info(f"Getting types from assembly: {assembly_name}")
136+
await safe_info(ctx, f"Getting types from assembly: {assembly_name}")
140137

141138
params = {"action": "get_types", "assembly_name": assembly_name}
142139
return handle_unity_command("runtime_compilation", params)
@@ -187,8 +184,7 @@ async def execute_with_roslyn(
187184
}
188185
```
189186
"""
190-
#await safe_info(ctx, f"Executing code with RoslynRuntimeCompiler: {class_name}.{method_name}")
191-
await ctx.info(f"Executing code with RoslynRuntimeCompiler: {class_name}.{method_name}")
187+
await safe_info(ctx, f"Executing code with RoslynRuntimeCompiler: {class_name}.{method_name}")
192188

193189
params = {
194190
"action": "execute_with_roslyn",
@@ -226,8 +222,7 @@ async def get_compilation_history(
226222
- Tracking execution flow
227223
- Auditing dynamic code changes
228224
"""
229-
#await safe_info(ctx, "Retrieving compilation history...")
230-
await ctx.info("Retrieving compilation history...")
225+
await safe_info(ctx, "Retrieving compilation history...")
231226

232227
params = {"action": "get_history"}
233228
return handle_unity_command("runtime_compilation", params)
@@ -250,8 +245,7 @@ async def save_compilation_history(
250245
- Share compilation sessions with team members
251246
- Archive successful code patterns
252247
"""
253-
#await safe_info(ctx, "Saving compilation history to file...")
254-
await ctx.info("Saving compilation history to file...")
248+
await safe_info(ctx, "Saving compilation history to file...")
255249

256250
params = {"action": "save_history"}
257251
return handle_unity_command("runtime_compilation", params)
@@ -269,8 +263,7 @@ async def clear_compilation_history(
269263
This removes all tracked compilations from memory but does not delete
270264
saved history files. Use this to start fresh or reduce memory usage.
271265
"""
272-
#await safe_info(ctx, "Clearing compilation history...")
273-
await ctx.info("Clearing compilation history...")
266+
await safe_info(ctx, "Clearing compilation history...")
274267

275268
params = {"action": "clear_history"}
276269
return handle_unity_command("runtime_compilation", params)

0 commit comments

Comments
 (0)