44using System . Linq ;
55using System . Text ;
66using System . Threading . Tasks ;
7- using Preagonal . Scripting . GS2Engine . Extensions ;
87using Preagonal . Scripting . GS2Engine . Enums ;
98using Preagonal . Scripting . GS2Engine . Exceptions ;
9+ using Preagonal . Scripting . GS2Engine . Extensions ;
1010using Preagonal . Scripting . GS2Engine . GS2 . ByteCode ;
1111using Preagonal . Scripting . GS2Engine . Models ;
1212using static Preagonal . Scripting . GS2Engine . Enums . StackEntryType ;
@@ -15,7 +15,7 @@ namespace Preagonal.Scripting.GS2Engine.GS2.Script;
1515
1616public class ScriptMachine
1717{
18- private readonly Script _script ;
18+ private readonly Script _script ;
1919 private readonly ScriptVariable _tempVariables = new ( ) ;
2020
2121 private delegate IStackEntry OpcodeHandler ( ScriptCom op , ref int index ) ;
@@ -220,7 +220,7 @@ out var command
220220 ( cmd as Script . Command ) ? . Invoke ( this , callParams . ToArray ( ) ) ?? 0 . ToStackEntry ( )
221221 ) ;
222222 break ;
223- case StackEntryType . ScriptProperty :
223+ case ScriptProperty :
224224 var scriptProperty = cmd as IScriptProperty ;
225225 var inst = opWith is { Count : > 0 } ? opWith . Peek ( ) . GetValue ( ) : null ;
226226
@@ -270,20 +270,21 @@ out var command
270270 */
271271
272272 op . LoopCount += 1 ;
273- index = _indexPos ;
274273 }
275274 else
276275 {
277276 op . Value = index ;
278277 op . VariableName = null ;
279- index = _indexPos ;
280278 }
281279
280+ index = _indexPos ;
281+
282282 break ;
283283 case Opcode . OP_JMP :
284284
285285 {
286286 //index = indexPos;
287+ Tools . DebugLine ( "" ) ;
287288 }
288289
289290 break ;
@@ -300,10 +301,10 @@ out var command
300301 stack . Push ( new StackEntry ( ArrayStart , null ) ) ;
301302 break ;
302303 case Opcode . OP_TYPE_TRUE :
303- stack . Push ( true . ToStackEntry ( ) ) ;
304+ stack . Push ( 1 . ToStackEntry ( ) ) ;
304305 break ;
305306 case Opcode . OP_TYPE_FALSE :
306- stack . Push ( false . ToStackEntry ( ) ) ;
307+ stack . Push ( 0 . ToStackEntry ( ) ) ;
307308 break ;
308309 case Opcode . OP_TYPE_NULL :
309310 stack . Push ( 0 . ToStackEntry ( ) ) ;
@@ -489,7 +490,7 @@ out var command
489490 stack . Push ( newArray . ToStackEntry ( ) ) ;
490491 break ;
491492 case Opcode . OP_SETARRAY :
492- break ;
493+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
493494 case Opcode . OP_INLINE_NEW :
494495 if ( stack . Count > 0 )
495496 {
@@ -501,7 +502,7 @@ out var command
501502 }
502503 break ;
503504 case Opcode . OP_MAKEVAR :
504- break ;
505+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
505506 case Opcode . OP_NEW_OBJECT :
506507 var newObject = stack . Pop ( ) ;
507508 var newObjectParam = stack . Pop ( ) ;
@@ -523,7 +524,7 @@ out var command
523524 case Opcode . OP_INLINE_CONDITIONAL :
524525
525526 var inlineConditional = stack . Pop ( ) ;
526- if ( inlineConditional . GetValue < bool > ( ) != false ) {
527+ if ( inlineConditional . GetValue < double > ( ) != 0 ) {
527528 inlineConditional . SetValue ( 1.0d ) ;
528529 }
529530 stack . Push ( inlineConditional ) ;
@@ -649,9 +650,12 @@ out var command
649650 stack . Push ( Math . Pow ( powB , powA ) . ToStackEntry ( ) ) ;
650651 break ;
651652 case Opcode . OP_NOT :
653+ var notVar = GetEntryValue < double > ( stack . Pop ( ) ) ;
654+
655+ stack . Push ( ( notVar == 0 ? true : false ) . ToStackEntry ( ) ) ;
652656 break ;
653657 case Opcode . OP_UNARYSUB :
654- break ;
658+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
655659 case Opcode . OP_EQ :
656660 var eq1 = GetEntryValue < object ? > ( stack . Pop ( ) ) ;
657661 var eq2 = GetEntryValue < object ? > ( stack . Pop ( ) ) ;
@@ -687,17 +691,35 @@ out var command
687691 stack . Push ( ( gteB >= gteA ) . ToStackEntry ( ) ) ;
688692 break ;
689693 case Opcode . OP_BWO :
690- break ;
694+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
691695 case Opcode . OP_BWA :
692- break ;
696+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
693697 case Opcode . OP_IN_RANGE :
694- break ;
698+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
695699 case Opcode . OP_IN_OBJ :
700+ var inObj = stack . Pop ( ) ;
701+ var isIn = stack . Pop ( ) ;
702+ if ( inObj . Type == StackEntryType . Array )
703+ {
704+ var inObjArray = inObj . GetValue < List < object ? > > ( ) ;
705+
706+ var boolVal = inObjArray ? . Contains ( isIn ? . GetValue ( ) ) ;
707+
708+ if ( ! boolVal . HasValue )
709+ {
710+ stack . Push ( 0.0d . ToStackEntry ( ) ) ;
711+ break ;
712+ }
713+
714+ stack . Push ( ( boolVal . Value ? 1.0d : 0.0d ) . ToStackEntry ( ) ) ;
715+ }
716+
717+ stack . Push ( 0.0d . ToStackEntry ( ) ) ;
696718 break ;
697719 case Opcode . OP_OBJ_INDEX :
698- break ;
720+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
699721 case Opcode . OP_OBJ_TYPE :
700- break ;
722+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
701723 case Opcode . OP_FORMAT :
702724 var format = stack . Pop ( ) ;
703725 var objects = stack . Select ( x => GetEntryValue < object > ( x ) ) . ToArray ( ) ;
@@ -706,12 +728,13 @@ out var command
706728 stack . Push ( formatted . ToStackEntry ( ) ) ;
707729 break ;
708730 case Opcode . OP_INT :
731+ stack . Push ( ( ( int ) GetEntryValue < double > ( stack . Pop ( ) ) ) . ToStackEntry ( ) ) ;
709732 break ;
710733 case Opcode . OP_ABS :
711734 stack . Push ( Math . Abs ( GetEntryValue < double > ( stack . Pop ( ) ) ) . ToStackEntry ( ) ) ;
712735 break ;
713736 case Opcode . OP_RANDOM :
714- break ;
737+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
715738 case Opcode . OP_SIN :
716739 var sinVal = Math . Sin ( GetEntryValue < double > ( stack . Pop ( ) ) ) ;
717740 sinVal = Math . Abs ( sinVal ) < 0.000001 ? 0.0f : sinVal ;
@@ -728,17 +751,17 @@ out var command
728751 stack . Push ( atanVal . ToStackEntry ( ) ) ;
729752 break ;
730753 case Opcode . OP_EXP :
731- break ;
754+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
732755 case Opcode . OP_LOG :
733- break ;
756+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
734757 case Opcode . OP_MIN :
735- break ;
758+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
736759 case Opcode . OP_MAX :
737- break ;
760+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
738761 case Opcode . OP_GETANGLE :
739- break ;
762+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
740763 case Opcode . OP_GETDIR :
741- break ;
764+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
742765 case Opcode . OP_VECX :
743766 var vecxDir = ( int ) stack . Pop ( ) . GetValue < double > ( ) ;
744767 var vecxVal = ( vecxDir % 4 ) switch
@@ -760,25 +783,29 @@ out var command
760783 stack . Push ( vecyVal . ToStackEntry ( ) ) ;
761784 break ;
762785 case Opcode . OP_OBJ_INDICES :
763- break ;
786+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
764787 case Opcode . OP_OBJ_LINK :
765- break ;
788+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
766789 case Opcode . OP_CHAR :
767- break ;
790+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
768791 case Opcode . OP_OBJ_TRIM :
769- break ;
792+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
770793 case Opcode . OP_OBJ_LENGTH :
771- break ;
794+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
772795 case Opcode . OP_OBJ_POS :
773- break ;
796+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
774797 case Opcode . OP_JOIN :
775798 var joinA = GetEntryValue < TString > ( stack . Pop ( ) ) ;
776799 var joinB = GetEntryValue < TString > ( stack . Pop ( ) ) ;
777800 stack . Push ( $ "{ joinB } { joinA } ". ToStackEntry ( ) ) ;
778801 break ;
779802 case Opcode . OP_OBJ_CHARAT :
780- break ;
803+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
781804 case Opcode . OP_OBJ_SUBSTR :
805+ var subStrLen = GetEntryValue < double > ( stack . Pop ( ) ) ;
806+ var subStrStart = GetEntryValue < double > ( stack . Pop ( ) ) ;
807+ var subStr = GetEntryValue < TString > ( stack . Pop ( ) ) ;
808+ stack . Push ( subStr ? . ToString ( ) . Substring ( ( int ) subStrStart , ( int ) subStrLen ) . ToStackEntry ( ) ?? 0 . ToStackEntry ( ) ) ;
782809 break ;
783810 case Opcode . OP_OBJ_STARTS :
784811 var obj = GetEntryValue < TString > ( stack . Pop ( ) ) ?? "" ;
@@ -788,13 +815,13 @@ out var command
788815 ) ;
789816 break ;
790817 case Opcode . OP_OBJ_ENDS :
791- break ;
818+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
792819 case Opcode . OP_OBJ_TOKENIZE :
793- break ;
820+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
794821 case Opcode . OP_TRANSLATE :
795- break ;
822+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
796823 case Opcode . OP_OBJ_POSITIONS :
797- break ;
824+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
798825 case Opcode . OP_OBJ_SIZE :
799826 var objSizeVar = GetEntry ( stack . Pop ( ) ) . GetValue ( ) ;
800827
@@ -841,25 +868,25 @@ out var command
841868
842869 break ;
843870 case Opcode . OP_ARRAY_MULTIDIM :
844- break ;
871+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
845872 case Opcode . OP_ARRAY_MULTIDIM_ASSIGN :
846- break ;
873+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
847874 case Opcode . OP_OBJ_SUBARRAY :
848- break ;
875+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
849876 case Opcode . OP_OBJ_ADDSTRING :
850- break ;
877+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
851878 case Opcode . OP_OBJ_DELETESTRING :
852- break ;
879+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
853880 case Opcode . OP_OBJ_REMOVESTRING :
854- break ;
881+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
855882 case Opcode . OP_OBJ_REPLACESTRING :
856- break ;
883+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
857884 case Opcode . OP_OBJ_INSERTSTRING :
858- break ;
885+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
859886 case Opcode . OP_OBJ_CLEAR :
860- break ;
887+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
861888 case Opcode . OP_ARRAY_NEW_MULTIDIM :
862- break ;
889+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
863890 case Opcode . OP_WITH :
864891 opWith ? . Push ( stack . Pop ( ) ) ;
865892 break ;
@@ -903,17 +930,17 @@ out var command
903930 ) ;
904931 break ;
905932 case Opcode . OP_PLAYERO :
906- break ;
933+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
907934 case Opcode . OP_LEVEL :
908- break ;
935+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
909936 case Opcode . OP_TEMP :
910937 stack . Push ( new StackEntry ( StackEntryType . Array , _tempVariables ) ) ;
911938 _useTemp = true ;
912939 break ;
913940 case Opcode . OP_PARAMS :
914- break ;
941+ break ; //throw new NotImplementedException($"OP({op.OpCode:D}): {op.OpCode:G}");
915942 case Opcode . OP_NUM_OPS :
916- break ;
943+ throw new NotImplementedException ( $ "OP( { op . OpCode : D } ): { op . OpCode : G } " ) ;
917944 }
918945 }
919946
0 commit comments