Skip to content

Commit

Permalink
added basic c# conversion for fsms and more advanced conversion for t…
Browse files Browse the repository at this point in the history
…he CallMethodProper and SetPlayerDataInt actions
  • Loading branch information
RedFrog6002 committed Apr 15, 2022
1 parent f786076 commit 331824e
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 0 deletions.
103 changes: 103 additions & 0 deletions FSMViewAvalonia2/ActionCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace FSMViewAvalonia2.CSharpConversion
{
public static class ActionCode
{
public static void WriteCodeForAction(FsmStateBuilder state, ActionScriptEntry action)
{
switch (action.Name)
{
case "CallMethodProper":
string code = GetOwnerDefaultString(action.Values[0].Item2 as FsmOwnerDefault) + ".GetComponent<" + (action.Values[1].Item2 as FsmString).value + ">()." + (action.Values[2].Item2 as FsmString).value + "(";
int length = action.Values.Count;
for (int i = 4; i < length; i++)
{
if (i != 4)
code += ", ";
code += GetOnlyValue(action.Values[i].Item2 as FsmVar);
}
code += ");";
state.AddMiddleCode(code);
break;
case "SetPlayerDataInt":
state.AddMiddleCode("PlayerData.instance.SetInt(" + action.Values[0].Item2 + ", " + action.Values[1].Item2 + ");");
break;
default:
state.AddMiddleCode(action.Name + ".DoSomething();");
break;
}
}

private static string GetOwnerDefaultString(FsmOwnerDefault owner)
{
if (owner.ownerOption == OwnerDefaultOption.UseOwner)
return "gameObject";
else
{
if (owner.gameObject.value.name == "")
return owner.gameObject.name;
else
return owner.gameObject.value.name;
}
}

private static string GetOnlyValue(FsmVar fsmVar)
{
if (fsmVar.variableName != "")
{
return fsmVar.variableName;
}
object value = null;
switch (fsmVar.type)
{
case VariableType.Float:
value = fsmVar.floatValue;
break;
case VariableType.Int:
value = fsmVar.intValue;
break;
case VariableType.Bool:
value = fsmVar.boolValue;
break;
case VariableType.String:
value = "\"" + fsmVar.stringValue + "\"";
break;
case VariableType.Vector2:
value = new Vector2() { x = fsmVar.vector4Value.x, y = fsmVar.vector4Value.y };
break;
case VariableType.Vector3:
value = new Vector3() { x = fsmVar.vector4Value.x, y = fsmVar.vector4Value.y, z = fsmVar.vector4Value.z };
break;
case VariableType.Object:
case VariableType.GameObject:
case VariableType.Material:
case VariableType.Texture:
value = fsmVar.objectReference;
break;
case VariableType.Color:
value = new UnityColor() { r = fsmVar.vector4Value.x, g = fsmVar.vector4Value.y, b = fsmVar.vector4Value.z, a = fsmVar.vector4Value.w };
break;
case VariableType.Rect:
value = new UnityRect() { x = fsmVar.vector4Value.x, y = fsmVar.vector4Value.y, width = fsmVar.vector4Value.z, height = fsmVar.vector4Value.w };
break;
case VariableType.Quaternion:
value = new Quaternion() { x = fsmVar.vector4Value.x, y = fsmVar.vector4Value.y, z = fsmVar.vector4Value.z, w = fsmVar.vector4Value.w };
break;
case VariableType.Array:
value = fsmVar.arrayValue;
break;
}
if (value != null)
{
if (value is NamedAssetPPtr namedPPtr && namedPPtr.name != "")
return namedPPtr.name;
else
return value.ToString();
}
return "null var";
}
}
}
2 changes: 2 additions & 0 deletions FSMViewAvalonia2/FSMInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public class FsmDataInstance
public Controls canvasControls;
public Matrix matrix;
public int dataVersion;
public string code;
}
public class FsmStateData
{
public string Name { get { return state.name; } }
public List<ActionScriptEntry> ActionData { get; set; }
public FsmState state;
public FsmNodeData node;
public string code;
}
public class FsmEventData
{
Expand Down
25 changes: 25 additions & 0 deletions FSMViewAvalonia2/FSMLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
using FSMViewAvalonia2.CSharpConversion;

namespace FSMViewAvalonia2
{
Expand Down Expand Up @@ -72,6 +73,8 @@ public FsmDataInstance LoadFSM(long id)
dataInstance.dataVersion = dataVersionField.GetValue().AsInt();
}

FsmClassBuilder classBuilder = new FsmClassBuilder(dataInstance.goName, dataInstance.fsmName);

dataInstance.states = new List<FsmStateData>();
for (int i = 0; i < states.GetChildrenCount(); i++)
{
Expand All @@ -83,6 +86,14 @@ public FsmDataInstance LoadFSM(long id)
GetActionData(stateData.ActionData, stateData.state.actionData, dataInstance.dataVersion);

dataInstance.states.Add(stateData);

FsmStateBuilder stateBuilder = classBuilder.CreateState(stateData.state.name);

int actions = stateData.ActionData.Count;
for (int j = 0; j < actions; j++)
ActionCode.WriteCodeForAction(stateBuilder, stateData.ActionData[j]);

stateData.code = stateBuilder.ToString();
}

dataInstance.events = new List<FsmEventData>();
Expand All @@ -98,6 +109,18 @@ public FsmDataInstance LoadFSM(long id)
dataInstance.variables = new List<FsmVariableData>();
GetVariableValues(dataInstance.variables, namer, variables);

int variablesCount = dataInstance.variables.Count;
for(int i = 0; i < variablesCount; i++)
{
FsmVariableData variableData = dataInstance.variables[i];

int variablesCount2 = variableData.Values.Count;
for(int j = 0; j < variablesCount2; j++)
{
classBuilder.AddField(variableData.Type, variableData.Values[j].Item1);
}
}

dataInstance.globalTransitions = new List<FsmNodeData>();
for (int i = 0; i < globalTransitions.GetChildrenCount(); i++)
{
Expand All @@ -115,6 +138,8 @@ public FsmDataInstance LoadFSM(long id)
dataInstance.globalTransitions.Add(node);
}

dataInstance.code = classBuilder.ToString();

//dataInstance.events = new List<FsmEventData>();
//for (int i = 0; i < events.GetChildrenCount(); i++)
//{
Expand Down
106 changes: 106 additions & 0 deletions FSMViewAvalonia2/FsmClassBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace FSMViewAvalonia2.CSharpConversion
{
public class FsmClassBuilder
{
private const string ClassMemberSpace = " ";

private List<(string, string)> Fields = new List<(string, string)>();
private List<FsmStateBuilder> States = new List<FsmStateBuilder>();
private string GOName;
private string FSMName;

public void AddField(string Type, string Name) => Fields.Add((Type, Name));

public FsmStateBuilder CreateState(string Name)
{
FsmStateBuilder state = new FsmStateBuilder(this, Name);
States.Add(state);
return state;
}

public FsmClassBuilder(string GOName, string FSMName)
{
this.GOName = GOName;
this.FSMName = FSMName;
}

public override string ToString()
{
string ret = "public class " + GOName + "_" + FSMName + "\n{\n";
int length = Fields.Count;
for (int i = 0; i < length; i++)
{
(string Type, string Name) = Fields[i];
ret += ClassMemberSpace + "public " + Type + " " + Name + "\n";
}
length = States.Count;
for (int i = 0; i < length; i++)
{
ret += ClassMemberSpace + "\n" + States[i].ToString();
}
ret += "}";
return ret;
}
}

public class FsmStateBuilder
{
private const string ClassMemberSpace = " ";
private const string MethodSpace = " ";

public FsmClassBuilder classBuilder;

private List<string> BeginningCode = new List<string>();
private List<string> MiddleCode = new List<string>();
private List<string> EndCode = new List<string>();
private string Name;
private bool IsEnumerator = false;

public void AddBeginningCode(string line) => BeginningCode.Add(line);
public void AddMiddleCode(string line) => MiddleCode.Add(line);
private void AddEndCode(string line) => EndCode.Add(line);
private void SetIsEnumerator() => IsEnumerator = true;

public FsmStateBuilder(FsmClassBuilder classBuilder, string Name)
{
this.classBuilder = classBuilder;
this.Name = Name;
}

private void WriteAllCode(ref string ret, List<string> list)
{
int length = list.Count;
for (int i = 0; i < length; i++)
{
ret += MethodSpace + list[i] + "\n";
}
}

public override string ToString()
{
string ret = ClassMemberSpace + (IsEnumerator ? "public IEnumerator " : "public void ") + Name + "()\n" + ClassMemberSpace + "{\n";
if (BeginningCode.Count > 0)
{
WriteAllCode(ref ret, BeginningCode);
if (MiddleCode.Count > 0)
ret += MethodSpace + "\n";
}
if (MiddleCode.Count > 0)
{
WriteAllCode(ref ret, MiddleCode);
if (EndCode.Count > 0)
ret += MethodSpace + "\n";
}
if (EndCode.Count > 0)
{
WriteAllCode(ref ret, EndCode);
}
ret += ClassMemberSpace + "}\n";
return ret;
}
}
}
10 changes: 10 additions & 0 deletions FSMViewAvalonia2/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@
</ScrollViewer>
</Grid>
</TabItem>
<TabItem Header="Code">
<Grid Background="#ffe5e5e5">
<ScrollViewer>
<StackPanel>
<MenuItem Header="View state code" IsEnabled="false" Name="switchToFull"/>
<TextBox IsReadOnly="true" Name="codeText"/>
</StackPanel>
</ScrollViewer>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Grid>
Expand Down
Loading

0 comments on commit 331824e

Please sign in to comment.