| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- //
- // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
- // LICENSE: Atomic Game Engine Editor and Tools EULA
- // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
- // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
- //
- #include <Atomic/IO/File.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/Core/StringUtils.h>
- #include "../JSBind.h"
- #include "../JSBFunction.h"
- #include "../JSBModule.h"
- #include "../JSBPackage.h"
- #include "../JSBEnum.h"
- #include "../JSBClass.h"
- #include "CSTypeHelper.h"
- #include "CSModuleWriter.h"
- #include "CSPackageWriter.h"
- namespace ToolCore
- {
- CSPackageWriter::CSPackageWriter(JSBPackage *package) : JSBPackageWriter(package)
- {
- }
- void CSPackageWriter::GenerateNativeFunctionThunk(String& sourceOut)
- {
- String source, line;
- source += IndentLine(ToString("\n\ntypedef struct AtomicNET%sThunk_s\n", package_->GetName().CString()));
- source += IndentLine("{\n");
- Indent();
- PODVector<JSBClass*>& allClasses = package_->GetAllClasses();
- for (unsigned i = 0; i < allClasses.Size(); i++)
- {
- JSBClass* cls = allClasses[i];
- PODVector<JSBFunction*>& functions = cls->GetFunctions();
- for (unsigned j = 0; j < functions.Size(); j++)
- {
- JSBFunction* function = functions[j];
- if (!CSTypeHelper::OmitFunction(function))
- {
- JSBClass* cls = function->GetClass();
- JSBPackage* package = cls->GetPackage();
- line = ToString("%s_%s_%s_Function ", package->GetName().CString(),
- cls->GetName().CString(),
- function->IsConstructor() ? "Constructor" : function->GetName().CString());
- line += ToString("__%s_%s_%s;\n", package->GetName().CString(),
- cls->GetName().CString(),
- function->IsConstructor() ? "Constructor" : function->GetName().CString());
- source += IndentLine(line);
- }
- }
- }
- Dedent();
- const char* packageName = package_->GetName().CString();
- source += IndentLine(ToString("\n} AtomicNET%sThunk_t;\n",packageName));
- source += IndentLine(ToString("extern AtomicNET%sThunk_t AtomicNET%sThunk;\n", packageName, packageName));
- source += IndentLine(ToString("extern bool AtomicNET%sThunkEnabled;\n", packageName));
- sourceOut += source;
- }
- void CSPackageWriter::GenNativeFunctionSignature(JSBFunction* function, String& sig)
- {
- JSBClass* klass = function->GetClass();
- Vector<JSBFunctionType*>& parameters = function->GetParameters();
- Vector<String> args;
- if (!function->IsConstructor())
- {
- args.Push(ToString("%s* self", klass->GetNativeName().CString()));
- }
- if (parameters.Size())
- {
- for (unsigned int i = 0; i < parameters.Size(); i++)
- {
- JSBFunctionType* ptype = parameters.At(i);
- // ignore "Context" parameters
- if (ptype->type_->asClassType())
- {
- JSBClassType* classType = ptype->type_->asClassType();
- JSBClass* klass = classType->class_;
- if (klass->GetName() == "Context")
- {
- continue;
- }
- args.Push(ToString("%s* %s", klass->GetNativeName().CString(), ptype->name_.CString()));
- }
- else
- {
- args.Push(CSTypeHelper::GetNativeTypeString(ptype) + " " + ptype->name_);
- }
- }
- }
- if (function->GetReturnClass() && function->GetReturnClass()->IsNumberArray())
- {
- args.Push(ToString("%s* returnValue", function->GetReturnClass()->GetNativeName().CString()));
- }
- sig.Join(args, ", ");
- }
- void CSPackageWriter::GenerateNativeFunctionTypeDefs(String& sourceOut)
- {
- // call typedefs
- Indent();
- String source, line;
- PODVector<JSBClass*>& allClasses = package_->GetAllClasses();
- for (unsigned i = 0; i < allClasses.Size(); i++)
- {
- JSBClass* cls = allClasses[i];
- PODVector<JSBFunction*>& functions = cls->GetFunctions();
- for (unsigned j = 0; j < functions.Size(); j++)
- {
- JSBFunction* function = functions[j];
- String returnType;
- line = CSTypeHelper::GetNativeFunctionSignature(function, returnType, true);
- if (line.Length())
- {
- line += ";\n";
- source += IndentLine(line);
- }
- }
- }
- Indent();
- sourceOut += source;
- }
- void CSPackageWriter::GenerateNativeHeader()
- {
- String source = "// This file was autogenerated by AtomicTool, changes will be lost\n\n";
- source += "#pragma once\n\n";
- if (package_->name_ != "Atomic")
- {
- source += "#include \"../../Atomic/Native/CSPackageAtomic.h\"\n";
- source += "using namespace Atomic;\n";
- }
- // enum includes
- PODVector<JSBHeader*> enumHeaders;
- for (unsigned i = 0; i < package_->modules_.Size(); i++)
- {
- Vector<SharedPtr<JSBEnum>> enums = package_->modules_[i]->GetEnums();
- for (unsigned j = 0; j < enums.Size(); j++)
- {
- JSBHeader* header = enums[j]->GetHeader();
- if (!enumHeaders.Contains(header))
- enumHeaders.Push(header);
- }
- }
- for (unsigned i = 0; i < enumHeaders.Size(); i++)
- {
- JSBHeader* header = enumHeaders[i];
- String headerPath = GetPath(header->GetFilePath());
- String headerfile = GetFileNameAndExtension(header->GetFilePath());
- JSBind* jsbind = header->GetSubsystem<JSBind>();
- headerPath.Replace(jsbind->GetSourceRootFolder() + "Source/", "");
- source.AppendWithFormat("#include <%s%s>\n", headerPath.CString(), headerfile.CString());
- }
- // forward declarations of package classes
- source += ToString("namespace %s\n{\n\n", package_->GetNamespace().CString());
- Indent();
- PODVector<JSBClass*>& allClasses = package_->GetAllClasses();
- for (unsigned i = 0; i < allClasses.Size(); i++)
- {
- JSBClass* cls = allClasses[i];
- source += IndentLine(ToString("class %s;\n", cls->GetNativeName().CString()));
- }
- Dedent();
- GenerateNativeFunctionTypeDefs(source);
- GenerateNativeFunctionThunk(source);
- source += "\n}\n";
- JSBind* jsbind = package_->GetSubsystem<JSBind>();
- String filepath = jsbind->GetDestNativeFolder() + "/CSPackage" + package_->name_ + ".h";
- File file(package_->GetContext());
- file.Open(filepath, FILE_WRITE);
- file.Write(source.CString(), source.Length());
- file.Close();
- }
- void CSPackageWriter::GenerateNativeThunkInit(String& sourceOut)
- {
- const char* packageName = package_->GetName().CString();
- String source, line;
- for (unsigned i = 0; i < package_->modules_.Size(); i++)
- {
- JSBModule* module = package_->modules_[i];
- const char* moduleName = module->GetName().CString();
- source.AppendWithFormat("extern \"C\" void csb_package_%s_init_%s_thunk ();\n", packageName, moduleName);
- }
- source.AppendWithFormat("\nvoid csb_package_init_%s_thunk ()\n{\n\n", packageName);
- Indent();
- for (unsigned i = 0; i < package_->modules_.Size(); i++)
- {
- JSBModule* module = package_->modules_[i];
- const char* moduleName = module->GetName().CString();
- line = ToString("csb_package_%s_init_%s_thunk ();\n", packageName, moduleName);
- source += IndentLine(line);
- }
- Dedent();
- source.Append("\n}\n");
- // Thunk Setter
- String thunkType = ToString("AtomicNET%sThunk_t", packageName);
- source += ToString("extern \"C\" void csb_package_set_%s_thunk (const %s *thunkIn)\n{\n", packageName, thunkType.CString());
- Indent();
- source += IndentLine(ToString("AtomicNET%sThunk = *thunkIn;\n", packageName));
- source += IndentLine(ToString("AtomicNET%sThunkEnabled = true;\n", packageName));
- Dedent();
- source += ToString("\n}\n");
- sourceOut += source;
- }
- void CSPackageWriter::GenerateNativeSource()
- {
- GenerateNativeHeader();
- String source = "// This file was autogenerated by AtomicTool, changes will be lost\n\n";
- const char* packageName = package_->name_.CString();
- String packageHeader = "CSPackage" + package_->name_ + ".h";
- source += ToString("#include \"%s\"\n", packageHeader.CString());
- if (package_->name_ != "Atomic")
- source += "using namespace Atomic;\n";
- // begin namespace
- source += ToString("using namespace %s;\n", packageName);
- source += ToString("namespace %s\n{\n", packageName);
- // thunk
- source += ToString("AtomicNET%sThunk_t AtomicNET%sThunk;\n", packageName, packageName);
- source += ToString("bool AtomicNET%sThunkEnabled = false;\n", packageName);
- // end of namespace
- source += "\n}\n";
- GenerateNativeThunkInit(source);
- JSBind* jsbind = package_->GetSubsystem<JSBind>();
- String filepath = jsbind->GetDestNativeFolder() + "/CSPackage" + package_->name_ + ".cpp";
- File file(package_->GetContext());
- file.Open(filepath, FILE_WRITE);
- file.Write(source.CString(), source.Length());
- file.Close();
- }
- void CSPackageWriter::GenerateManagedSource()
- {
- }
- void CSPackageWriter::GenerateSource()
- {
- GenerateNativeSource();
- GenerateManagedSource();
- for (unsigned i = 0; i < package_->modules_.Size(); i++)
- {
- CSModuleWriter writer(package_->modules_[i]);
- writer.GenerateSource();
- }
- }
- }
|