C
C#6mo ago
TheSnowOwl

Trying to compile a script at runtime

im trying to compile a script at runtime using CSharpCompilation.Emit this is the script i want to compile
namespace WinterRose.Generated.Serializer_WinterRose_Vectors_Vector2
{
using System;
using WinterRose.Vectors;
using WinterRose.Serialization.Workers;
using WinterRose.Serialization;
using System.Collections.Generic;
using System.Text;

public class GeneratedSerializer_Vector2
{
public StringBuilder Serialize(WinterRose.Vectors.Vector2 obj, SerializerSettings settings, Int32 depth)
{
StringBuilder builder = new();
builder.Append("@0VmVjdG9yMi0tV2ludGVyUm9zZS5WZWN0b3JzLS1XaW50ZXJSb3NlLCBWZXJzaW9uPTEuMy4zLjEsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbA==");
builder.Append($"#0x|0{SnowSerializerWorkers.SerializeField(obj.x, "x", typeof(Single), depth, settings)}");
builder.Append($"#0y|0{SnowSerializerWorkers.SerializeField(obj.y, "y", typeof(Single), depth, settings)}");
return builder;
}

public Vector2 Deserialize(string data, SerializerSettings settings, Int32 depth)
{
Vector2 result = new();
Dictionary<string, string?> values = new();
string[] splits = data.Split($"#{depth}");
foreach (string item in splits)
{
string[] split = item.Split("|{depth}");
values.Add(split[0], split[1] ?? null);
}
foreach (var item in values)
{
switch (item.Key)
{
case "x":
result.x = SnowSerializerWorkers.DeserializeField<Single>(values["x"], typeof(Single), depth);
break;
case "y":
result.y = SnowSerializerWorkers.DeserializeField<Single>(values["y"], typeof(Single), depth);
break;
}
}
return result;
}
}
}
namespace WinterRose.Generated.Serializer_WinterRose_Vectors_Vector2
{
using System;
using WinterRose.Vectors;
using WinterRose.Serialization.Workers;
using WinterRose.Serialization;
using System.Collections.Generic;
using System.Text;

public class GeneratedSerializer_Vector2
{
public StringBuilder Serialize(WinterRose.Vectors.Vector2 obj, SerializerSettings settings, Int32 depth)
{
StringBuilder builder = new();
builder.Append("@0VmVjdG9yMi0tV2ludGVyUm9zZS5WZWN0b3JzLS1XaW50ZXJSb3NlLCBWZXJzaW9uPTEuMy4zLjEsIEN1bHR1cmU9bmV1dHJhbCwgUHVibGljS2V5VG9rZW49bnVsbA==");
builder.Append($"#0x|0{SnowSerializerWorkers.SerializeField(obj.x, "x", typeof(Single), depth, settings)}");
builder.Append($"#0y|0{SnowSerializerWorkers.SerializeField(obj.y, "y", typeof(Single), depth, settings)}");
return builder;
}

public Vector2 Deserialize(string data, SerializerSettings settings, Int32 depth)
{
Vector2 result = new();
Dictionary<string, string?> values = new();
string[] splits = data.Split($"#{depth}");
foreach (string item in splits)
{
string[] split = item.Split("|{depth}");
values.Add(split[0], split[1] ?? null);
}
foreach (var item in values)
{
switch (item.Key)
{
case "x":
result.x = SnowSerializerWorkers.DeserializeField<Single>(values["x"], typeof(Single), depth);
break;
case "y":
result.y = SnowSerializerWorkers.DeserializeField<Single>(values["y"], typeof(Single), depth);
break;
}
}
return result;
}
}
}
certainly it would be a lot better to actually use source generators, however my project requires the ability to generate code from scripts that are also compiled at runtime. however this specific script is causing me errors that i cant seem to find. errors like switch statments missing, commas missing, semicolons missing, and like 200 of these, so i really have no idea i hope someone here has some ideas of what i could do. i already tried restarting visual studio, that didnt work
2 Replies
TheSnowOwl
TheSnowOwl6mo ago
these are all the diagnostics (28,1): error CS1733: Expected expression (27,1): error CS1733: Expected expression (43,1): error CS1733: Expected expression (27,1): error CS1733: Expected expression (28,1): error CS1733: Expected expression (28,1): error CS1002: ; expected (28,1): error CS1002: ; expected (43,1): error CS1002: ; expected (29,1): error CS1513: } expected (29,1): error CS1513: } expected (44,1): error CS1513: } expected (31,1): error CS1525: Invalid expression term '}' (31,1): error CS1525: Invalid expression term '}' (46,1): error CS1525: Invalid expression term '}' (27,1): error CS1002: ; expected (28,1): error CS1513: } expected (30,1): error CS1525: Invalid expression term '}' (27,1): error CS1002: ; expected (28,1): error CS1513: } expected (30,1): error CS1525: Invalid expression term '}' (31,1): error CS1002: ; expected (33,1): error CS1733: Expected expression (31,1): error CS1002: ; expected (33,1): error CS1002: ; expected (34,1): error CS1513: } expected (35,1): error CS1514: { expected (46,1): error CS1002: ; expected (35,1): error CS1513: } expected (33,1): error CS1733: Expected expression (36,1): error CS1513: } expected (33,1): error CS1002: ; expected (30,1): error CS1002: ; expected (48,1): error CS1733: Expected expression (32,1): error CS1733: Expected expression (30,1): error CS1002: ; expected (36,1): error CS1003: Syntax error, 'switch' expected (34,1): error CS1513: } expected (48,1): error CS1002: ; expected (32,1): error CS1002: ; expected (37,1): error CS1513: } expected (49,1): error CS1513: } expected (35,1): error CS1514: { expected (33,1): error CS1513: } expected (32,1): error CS1733: Expected expression (35,1): error CS1513: } expected (32,1): error CS1002: ; expected (39,1): error CS1003: Syntax error, 'switch' expected (50,1): error CS1514: { expected (34,1): error CS1514: { expected (36,1): error CS1513: } expected (33,1): error CS1513: } expected (50,1): error CS1513: } expected (34,1): error CS1513: } expected (40,1): error CS1513: } expected (36,1): error CS1003: Syntax error, 'switch' expected (51,1): error CS1513: } expected (35,1): error CS1513: } expected (34,1): error CS1514: { expected (42,1): error CS1003: Syntax error, 'switch' expected (37,1): error CS1513: } expected (51,1): error CS1003: Syntax error, 'switch' expected (34,1): error CS1513: } expected (35,1): error CS1003: Syntax error, 'switch' expected (43,1): error CS1513: } expected (39,1): error CS1003: Syntax error, 'switch' expected (52,1): error CS1513: } expected (35,1): error CS1513: } expected (36,1): error CS1513: } expected (45,1): error CS1525: Invalid expression term '}' (40,1): error CS1513: } expected (35,1): error CS1003: Syntax error, 'switch' expected (54,1): error CS1003: Syntax error, 'switch' expected (38,1): error CS1003: Syntax error, 'switch' expected (36,1): error CS1513: } expected (55,1): error CS1513: } expected (45,1): error CS1002: ; expected (42,1): error CS1003: Syntax error, 'switch' expected (39,1): error CS1513: } expected (57,1): error CS1003: Syntax error, 'switch' expected (38,1): error CS1003: Syntax error, 'switch' expected (43,1): error CS1513: } expected (46,1): error CS1525: Invalid expression term '}' (41,1): error CS1525: Invalid expression term '}' (58,1): error CS1513: } expected (39,1): error CS1513: } expected (46,1): error CS1002: ; expected (45,1): error CS1525: Invalid expression term '}' (41,1): error CS1002: ; expected (60,1): error CS1003: Syntax error, 'switch' expected (41,1): error CS1525: Invalid expression term '}' (45,1): error CS1002: ; expected (41,1): error CS1002: ; expected (61,1): error CS1513: } expected (42,1): error CS1525: Invalid expression term '}' (46,1): error CS1525: Invalid expression term '}' (42,1): error CS1525: Invalid expression term '}' (63,1): error CS1003: Syntax error, 'switch' expected (46,1): error CS1002: ; expected (42,1): error CS1002: ; expected (42,1): error CS1002: ; expected (64,1): error CS1513: } expected (66,1): error CS1003: Syntax error, 'switch' expected (67,1): error CS1513: } expected (69,1): error CS1003: Syntax error, 'switch' expected (70,1): error CS1513: } expected (72,1): error CS1003: Syntax error, 'switch' expected (73,1): error CS1513: } expected (75,1): error CS1003: Syntax error, 'switch' expected (76,1): error CS1513: } expected (78,1): error CS1003: Syntax error, 'switch' expected (79,1): error CS1513: } expected (81,1): error CS1003: Syntax error, 'switch' expected (82,1): error CS1513: } expected (84,1): error CS1003: Syntax error, 'switch' expected (85,1): error CS1513: } expected (87,1): error CS1003: Syntax error, 'switch' expected (88,1): error CS1513: } expected (90,1): error CS1003: Syntax error, 'switch' expected (91,1): error CS1513: } expected (93,1): error CS1003: Syntax error, 'switch' expected (94,1): error CS1513: } expected (96,1): error CS1003: Syntax error, 'switch' expected (97,1): error CS1513: } expected (99,1): error CS1003: Syntax error, 'switch' expected (100,1): error CS1513: } expected (102,1): error CS1525: Invalid expression term '}' (102,1): error CS1002: ; expected (103,1): error CS1525: Invalid expression term '}' (103,1): error CS1002: ; expected
jcotton42
jcotton426mo ago
Have you looked at the script in an editor like VS or VSCode? Cleaning up the formatting will probably also help