vm better error handling, cleanup

This commit is contained in:
Nahuel Rocchetti 2023-08-25 09:07:51 -03:00
parent e7d5b93b95
commit 4358bcfe5b
2 changed files with 48 additions and 34 deletions

View file

@ -21,44 +21,58 @@ namespace OpenTS2.SimAntics
// TODO - still super incomplete, just enough to run basic scripts.
public short GetData(VMDataSource source, short dataIndex)
{
return source switch
try
{
VMDataSource.Literal => dataIndex,
VMDataSource.Temps => Entity.Temps[dataIndex],
VMDataSource.Params => StackFrame.Arguments[dataIndex],
VMDataSource.StackObjectID => StackFrame.StackObjectID,
VMDataSource.TempByTempIndex => Entity.Temps[Entity.Temps[dataIndex]],
VMDataSource.StackObjectsTemp => StackObjectEntity.Temps[dataIndex],
VMDataSource.Local => StackFrame.Locals[dataIndex],
VMDataSource.StackObjectsDefinition => (short)StackObjectEntity.ObjectDefinition.Fields[dataIndex],
_ => throw new SimAnticsException("Attempted to retrieve a variable from an unknown data source.", StackFrame)
};
return source switch
{
VMDataSource.Literal => dataIndex,
VMDataSource.Temps => Entity.Temps[dataIndex],
VMDataSource.Params => StackFrame.Arguments[dataIndex],
VMDataSource.StackObjectID => StackFrame.StackObjectID,
VMDataSource.TempByTempIndex => Entity.Temps[Entity.Temps[dataIndex]],
VMDataSource.StackObjectsTemp => StackObjectEntity.Temps[dataIndex],
VMDataSource.Local => StackFrame.Locals[dataIndex],
VMDataSource.StackObjectsDefinition => (short)StackObjectEntity.ObjectDefinition.Fields[dataIndex],
_ => throw new SimAnticsException("Attempted to retrieve a variable from an out of range data source.", StackFrame)
};
}
catch (ArgumentOutOfRangeException)
{
throw new SimAnticsException("Attempted to retrieve a variable from an out of range data index.", StackFrame);
}
}
public void SetData(VMDataSource source, short dataIndex, short value)
{
switch(source)
try
{
case VMDataSource.Temps:
Entity.Temps[dataIndex] = value;
return;
case VMDataSource.Params:
StackFrame.Arguments[dataIndex] = value;
return;
case VMDataSource.StackObjectID:
StackFrame.StackObjectID = value;
return;
case VMDataSource.TempByTempIndex:
Entity.Temps[Entity.Temps[dataIndex]] = value;
return;
case VMDataSource.StackObjectsTemp:
StackObjectEntity.Temps[dataIndex] = value;
return;
case VMDataSource.Local:
StackFrame.Locals[dataIndex] = value;
return;
switch (source)
{
case VMDataSource.Temps:
Entity.Temps[dataIndex] = value;
return;
case VMDataSource.Params:
StackFrame.Arguments[dataIndex] = value;
return;
case VMDataSource.StackObjectID:
StackFrame.StackObjectID = value;
return;
case VMDataSource.TempByTempIndex:
Entity.Temps[Entity.Temps[dataIndex]] = value;
return;
case VMDataSource.StackObjectsTemp:
StackObjectEntity.Temps[dataIndex] = value;
return;
case VMDataSource.Local:
StackFrame.Locals[dataIndex] = value;
return;
}
throw new SimAnticsException("Attempted to modify a variable from an out of range data source.", StackFrame);
}
catch (ArgumentOutOfRangeException)
{
throw new SimAnticsException("Attempted to modify a variable from an out of range data index.", StackFrame);
}
throw new SimAnticsException("Attempted to modify a variable from an unknown data source.", StackFrame);
}
}
}

View file

@ -42,8 +42,6 @@ namespace OpenTS2.SimAntics
if (CurrentContinueHandler != null)
{
result = CurrentContinueHandler.Tick();
if (result == VMExitCode.Continue)
return result;
}
else
result = ExecuteNode(currentNode);
@ -124,6 +122,8 @@ namespace OpenTS2.SimAntics
Stack.Frames.Push(newStackFrame);
return newStackFrame.Tick();
}
else
throw new SimAnticsException("Attempted to GoSub to invalid tree, or called unknown primitive.", this);
}
}
return VMExitCode.False;
@ -237,7 +237,7 @@ namespace OpenTS2.SimAntics
public void SetCurrentNode(int nodeIndex)
{
if (nodeIndex > BHAV.Nodes.Count || nodeIndex < 0)
if (nodeIndex >= BHAV.Nodes.Count || nodeIndex < 0)
{
throw new SimAnticsException("Attempted to transition to node out of range.", this);
}