Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Lactozilla committed Dec 14, 2023
2 parents 00766ac + 98b5388 commit f769ed1
Show file tree
Hide file tree
Showing 33 changed files with 1,811 additions and 1,714 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(HatchGameEngine VERSION 1.0)
project(HatchGameEngine VERSION 1.1.0)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 11)
Expand Down
5 changes: 3 additions & 2 deletions VisualC/HatchGameEngine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ CD ..</Command>
<ClCompile Include="..\source\engine\bytecode\TypeImpl\ArrayImpl.cpp" />
<ClCompile Include="..\source\engine\bytecode\TypeImpl\FunctionImpl.cpp" />
<ClCompile Include="..\source\engine\bytecode\TypeImpl\MapImpl.cpp" />
<ClCompile Include="..\source\engine\bytecode\BytecodeObject.cpp" />
<ClCompile Include="..\source\engine\bytecode\BytecodeObjectManager.cpp" />
<ClCompile Include="..\source\engine\bytecode\Bytecode.cpp" />
<ClCompile Include="..\source\engine\bytecode\Compiler.cpp" />
<ClCompile Include="..\source\engine\bytecode\GarbageCollector.cpp" />
<ClCompile Include="..\source\engine\bytecode\ScriptEntity.cpp" />
<ClCompile Include="..\source\engine\bytecode\ScriptManager.cpp" />
<ClCompile Include="..\source\engine\bytecode\SourceFileMap.cpp" />
<ClCompile Include="..\source\engine\bytecode\StandardLibrary.cpp" />
<ClCompile Include="..\source\engine\bytecode\Types.cpp" />
Expand Down
11 changes: 7 additions & 4 deletions VisualC/HatchGameEngine.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@
<ClCompile Include="..\source\engine\bytecode\TypeImpl\MapImpl.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\source\engine\bytecode\BytecodeObject.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\source\engine\bytecode\BytecodeObjectManager.cpp">
<ClCompile Include="..\source\engine\bytecode\Bytecode.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\source\engine\bytecode\Compiler.cpp">
Expand All @@ -48,6 +45,12 @@
<ClCompile Include="..\source\engine\bytecode\GarbageCollector.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\source\engine\bytecode\ScriptEntity.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\source\engine\bytecode\ScriptManager.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\source\engine\bytecode\SourceFileMap.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down
606 changes: 310 additions & 296 deletions guides/Documentation.htm

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions source/Engine/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class Application {
#include <Engine/Application.h>
#include <Engine/Graphics.h>

#include <Engine/Bytecode/BytecodeObjectManager.h>
#include <Engine/Bytecode/BytecodeObject.h>
#include <Engine/Bytecode/ScriptManager.h>
#include <Engine/Bytecode/ScriptEntity.h>
#include <Engine/Bytecode/GarbageCollector.h>
#include <Engine/Bytecode/SourceFileMap.h>
#include <Engine/Diagnostics/Clock.h>
Expand Down Expand Up @@ -516,8 +516,8 @@ PRIVATE STATIC void Application::Restart() {
});
Graphics::SpriteSheetTextureMap->Clear();

BytecodeObjectManager::LoadAllClasses = false;
BytecodeObject::DisableAutoAnimate = false;
ScriptManager::LoadAllClasses = false;
ScriptEntity::DisableAutoAnimate = false;

Graphics::Reset();

Expand Down Expand Up @@ -1257,7 +1257,7 @@ PRIVATE STATIC void Application::LoadGameConfig() {
// Read engine settings
node = XMLParser::SearchNode(root, "engine");
if (node) {
ParseGameConfigBool(node, "loadAllClasses", BytecodeObjectManager::LoadAllClasses);
ParseGameConfigBool(node, "loadAllClasses", ScriptManager::LoadAllClasses);
ParseGameConfigBool(node, "useSoftwareRenderer", Graphics::UseSoftwareRenderer);
ParseGameConfigBool(node, "enablePaletteUsage", Graphics::UsePalettes);
}
Expand Down
199 changes: 199 additions & 0 deletions source/Engine/Bytecode/Bytecode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
#if INTERFACE
#include <Engine/Bytecode/Types.h>
#include <Engine/Bytecode/CompilerEnums.h>

class Bytecode {
public:
static const char* Magic;
static Uint32 LatestVersion;
static vector<const char*> FunctionNames;

vector<ObjFunction*> Functions;

Uint8 Version;
bool HasDebugInfo = false;
const char* SourceFilename = nullptr;
};
#endif

#include <Engine/Bytecode/Bytecode.h>
#include <Engine/IO/MemoryStream.h>
#include <Engine/Utilities/StringUtils.h>

const char* Bytecode::Magic = "HTVM";
Uint32 Bytecode::LatestVersion = 0;
vector<const char*> Bytecode::FunctionNames{ "<anonymous-fn>", "main" };

PUBLIC Bytecode::Bytecode() {
Version = LatestVersion;
}

PUBLIC Bytecode::~Bytecode() {
Memory::Free((void*)SourceFilename);
}

PUBLIC bool Bytecode::Read(BytecodeContainer bytecode, HashMap<char*>* tokens) {
MemoryStream* stream = MemoryStream::New(bytecode.Data, bytecode.Size);
if (!stream)
return false;

Uint8 magic[4];
stream->ReadBytes(magic, 4);
if (memcmp(Bytecode::Magic, magic, 4) != 0) {
Log::Print(Log::LOG_ERROR, "Incorrect magic!");
stream->Close();
return false;
}

Version = stream->ReadByte();
Uint8 opts = stream->ReadByte();
stream->Skip(1);
stream->Skip(1);

HasDebugInfo = opts & 1;

int chunkCount = stream->ReadInt32();
if (!chunkCount)
return false;

for (int i = 0; i < chunkCount; i++) {
int length = stream->ReadInt32();
int arity = stream->ReadInt32();
Uint32 hash = stream->ReadUInt32();

ObjFunction* function = NewFunction();
function->Arity = arity;
function->NameHash = hash;
function->Chunk.Count = length;
function->Chunk.OwnsMemory = false;

function->Chunk.Code = stream->pointer;
stream->Skip(length * sizeof(Uint8));

if (HasDebugInfo) {
function->Chunk.Lines = (int*)stream->pointer;
stream->Skip(length * sizeof(int));
}

int constantCount = stream->ReadInt32();
for (int c = 0; c < constantCount; c++) {
Uint8 type = stream->ReadByte();
switch (type) {
case VAL_INTEGER:
function->Chunk.AddConstant(INTEGER_VAL(stream->ReadInt32()));
break;
case VAL_DECIMAL:
function->Chunk.AddConstant(DECIMAL_VAL(stream->ReadFloat()));
break;
case VAL_OBJECT:
function->Chunk.AddConstant(OBJECT_VAL(TakeString(stream->ReadString())));
break;
}
}

Functions.push_back(function);
}

if (HasDebugInfo) {
int tokenCount = stream->ReadInt32();
for (int t = 0; t < tokenCount; t++) {
char* string = stream->ReadString();
if (!tokens)
stream->SkipString();
else {
Uint32 hash = Murmur::EncryptString(string);
if (!tokens->Exists(hash))
tokens->Put(hash, string);
else
Memory::Free(string);
}
}

if (tokens) {
for (ObjFunction* function : Functions) {
if (tokens->Exists(function->NameHash))
function->Name = CopyString(tokens->Get(function->NameHash));
}
}
}

bool hasSourceFilename = opts & 2;
if (hasSourceFilename) {
SourceFilename = stream->ReadString();

ObjString* srcFilename = CopyString(SourceFilename);
for (ObjFunction* function : Functions)
function->SourceFilename = srcFilename;
}

return true;
}

PUBLIC void Bytecode::Write(Stream* stream, const char* sourceFilename, HashMap<Token>* tokenMap) {
bool hasSourceFilename = sourceFilename != nullptr;

stream->WriteBytes((char*)Bytecode::Magic, 4);
stream->WriteByte(Version);
stream->WriteByte((hasSourceFilename << 1) | HasDebugInfo);
stream->WriteByte(0x00);
stream->WriteByte(0x00);

int chunkCount = (int)Functions.size();

stream->WriteUInt32(chunkCount);
for (int c = 0; c < chunkCount; c++) {
int arity = Functions[c]->Arity;
Chunk* chunk = &Functions[c]->Chunk;

stream->WriteUInt32(chunk->Count);
stream->WriteUInt32(arity);
stream->WriteUInt32(Murmur::EncryptString(Functions[c]->Name->Chars));

stream->WriteBytes(chunk->Code, chunk->Count);
if (HasDebugInfo) {
stream->WriteBytes(chunk->Lines, chunk->Count * sizeof(int));
}

int constSize = (int)chunk->Constants->size();
stream->WriteUInt32(constSize);
for (int i = 0; i < constSize; i++) {
VMValue constt = (*chunk->Constants)[i];
Uint8 type = (Uint8)constt.Type;
stream->WriteByte(type);

switch (type) {
case VAL_INTEGER:
stream->WriteBytes(&AS_INTEGER(constt), sizeof(int));
break;
case VAL_DECIMAL:
stream->WriteBytes(&AS_DECIMAL(constt), sizeof(float));
break;
case VAL_OBJECT:
if (OBJECT_TYPE(constt) == OBJ_STRING) {
ObjString* str = AS_STRING(constt);
stream->WriteBytes(str->Chars, str->Length + 1);
}
else {
printf("Unsupported object type...Chief. (%s)\n", GetObjectTypeString(OBJECT_TYPE(constt)));
stream->WriteByte(0);
}
break;
}
}
}

// Add tokens
if (HasDebugInfo && tokenMap) {
stream->WriteUInt32(tokenMap->Count + FunctionNames.size());
std::for_each(FunctionNames.begin(), FunctionNames.end(), [stream](const char* name) {
stream->WriteBytes((void*)name, strlen(name) + 1);
});
tokenMap->WithAll([stream](Uint32, Token t) -> void {
stream->WriteBytes(t.Start, t.Length);
stream->WriteByte(0); // NULL terminate
});
}

if (hasSourceFilename)
stream->WriteBytes((void*)sourceFilename, strlen(sourceFilename) + 1);
}
Loading

0 comments on commit f769ed1

Please sign in to comment.