forked from nesrak1/FSMViewAvalonia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added basic c# conversion for fsms and more advanced conversion for t…
…he CallMethodProper and SetPlayerDataInt actions
- Loading branch information
1 parent
f786076
commit 331824e
Showing
6 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.