| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- //
- // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include <Atomic/IO/File.h>
- #include <Atomic/IO/FileSystem.h>
- #include "../JSBind.h"
- #include "../JSBPackage.h"
- #include "../JSBModule.h"
- #include "../JSBEnum.h"
- #include "../JSBClass.h"
- #include "../JSBFunction.h"
- #include "CSTypeHelper.h"
- #include "CSClassWriter.h"
- #include "CSModuleWriter.h"
- namespace ToolCore
- {
- CSModuleWriter::CSModuleWriter(JSBModule *module) : JSBModuleWriter(module)
- {
- }
- void CSModuleWriter::WriteIncludes(String& source)
- {
- Vector<String>& includes = module_->includes_;
- for (unsigned i = 0; i < includes.Size(); i++)
- {
- if (includes[i].StartsWith("<"))
- source.AppendWithFormat("#include %s\n", includes[i].CString());
- else
- source.AppendWithFormat("#include \"%s\"\n", includes[i].CString());
- }
- Vector<JSBHeader*> allheaders;
- HashMap<StringHash, SharedPtr<JSBEnum> >::Iterator eitr = module_->enums_.Begin();
- while (eitr != module_->enums_.End())
- {
- allheaders.Push(eitr->second_->GetHeader());
- eitr++;
- }
- HashMap<StringHash, SharedPtr<JSBClass> >::Iterator citr = module_->classes_.Begin();
- while (citr != module_->classes_.End())
- {
- allheaders.Push(citr->second_->GetHeader());
- citr++;
- }
- Vector<JSBHeader*> included;
- for (unsigned i = 0; i < allheaders.Size(); i++)
- {
- JSBHeader* header = allheaders.At(i);
- if (included.Contains(header))
- continue;
- 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());
- included.Push(header);
- }
- source += "\n#include <Atomic/Script/ScriptVector.h>\n";
- source += ToString("\n#include \"CSPackage%s.h\"\n", module_->GetPackage()->GetName().CString());
- }
- void CSModuleWriter::GenerateNativeSource()
- {
- String source = "// This file was autogenerated by JSBind, changes will be lost\n";
- String moduleGuard = module_->GetModuleDefineGuard();
- if (moduleGuard.Length())
- {
- source += ToString("\n%s\n", moduleGuard.CString());
- }
- source += "#ifdef ATOMIC_PLATFORM_WINDOWS\n";
- source += "#pragma warning(disable: 4244) // possible loss of data\n";
- source += "#define ATOMIC_EXPORT_API __declspec(dllexport)\n";
- source += "#else\n";
- source += "#define ATOMIC_EXPORT_API\n";
- source += "#endif\n";
- if (module_->Requires("3D"))
- {
- source += "#ifdef ATOMIC_3D\n";
- }
- WriteIncludes(source);
- source += "\n#include <AtomicNET/NETNative/NETCore.h>\n";
- String ns = module_->GetPackage()->GetNamespace();
- source += "\n\nusing namespace " + ns + ";\n\n";
- source += "\n\nextern \"C\" \n{\n \n";
- source += "// Begin Class Declarations\n";
- source += "// End Class Declarations\n\n";
- source += "// Begin Classes\n";
- Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
- for (unsigned i = 0; i < classes.Size(); i++)
- {
- String classGuard = module_->GetClassDefineGuard(classes[i]->GetNativeName());
- if (classGuard.Length())
- {
- source += ToString("\n%s\n\n", classGuard.CString());
- }
- CSClassWriter clsWriter(classes[i]);
- clsWriter.GenerateNativeSource(source);
- if (classGuard.Length())
- {
- source += "#endif\n\n";
- }
- }
- source += "// End Classes\n\n";
- // end Atomic namespace
- source += "\n}\n";
- if (module_->Requires("3D"))
- {
- source += "#endif //ATOMIC_3D\n";
- }
- if (moduleGuard.Length())
- {
- source += "\n#endif\n";
- }
- JSBind* jsbind = module_->GetSubsystem<JSBind>();
- String filepath = jsbind->GetDestNativeFolder() + "/CSModule" + module_->name_ + ".cpp";
- File file(module_->GetContext());
- file.Open(filepath, FILE_WRITE);
- file.Write(source.CString(), source.Length());
- file.Close();
- }
- String CSModuleWriter::GetManagedPrimitiveType(JSBPrimitiveType* ptype)
- {
- if (ptype->kind_ == JSBPrimitiveType::Bool)
- return "bool";
- if (ptype->kind_ == JSBPrimitiveType::Int && ptype->isUnsigned_)
- return "uint";
- else if (ptype->kind_ == JSBPrimitiveType::Int)
- return "int";
- if (ptype->kind_ == JSBPrimitiveType::Float)
- return "float";
- return "int";
- }
- void CSModuleWriter::GenerateManagedClasses(String& source)
- {
- Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
- for (unsigned i = 0; i < classes.Size(); i++)
- {
- JSBClass* klass = classes.At(i);
- if (klass->IsNumberArray())
- continue;
- CSClassWriter clsWriter(klass);
- clsWriter.GenerateManagedSource(source);
- }
- }
- void CSModuleWriter::GenerateManagedEnumsAndConstants(String& source)
- {
- Vector<SharedPtr<JSBEnum>> enums = module_->enums_.Values();
- Indent();
- for (unsigned i = 0; i < enums.Size(); i++)
- {
- JSBEnum* jenum = enums[i];
- source += "\n";
- String line = "public enum " + jenum->GetName() + "\n";
- source += IndentLine(line);
- source += IndentLine("{\n");
- HashMap<String, String>& values = jenum->GetValues();
- HashMap<String, String>::ConstIterator itr = values.Begin();
- Indent();
- while (itr != values.End())
- {
- String name = (*itr).first_;
- String value = (*itr).second_;
- // BodyType2D enum order is assigned in RigidBody2D.h in incorrect order
- if (jenum->GetName() == "BodyType2D")
- {
- if (name == "BT_KINEMATIC")
- value = "1";
- else if (name == "BT_DYNAMIC")
- value = "2";
- }
- if (value.Length())
- {
- line = name + " = " + value;
- }
- else
- {
- line = name;
- }
- itr++;
- if (itr != values.End())
- line += ",";
- line += "\n";
- source += IndentLine(line);
- }
- Dedent();
- source += IndentLine("}\n");
- }
- // constants
- HashMap<String, JSBModule::Constant>& constants = module_->GetConstants();
- if (constants.Size())
- {
- source += "\n";
- String line = "public static partial class Constants\n";
- source += IndentLine(line);
- source += IndentLine("{\n");
- const Vector<String>& constantsName = constants.Keys();
- Indent();
- for (unsigned i = 0; i < constantsName.Size(); i++)
- {
- const String& cname = constantsName.At(i);
- JSBModule::Constant& constant = constants[cname];
- String managedType = GetManagedPrimitiveType(constant.type);
- String value = constant.value;
- if (!value.Length())
- continue;
- //static const unsigned M_MIN_UNSIGNED = 0x00000000;
- // /static const unsigned M_MAX_UNSIGNED = 0xffffffff;
- if (cname == "M_MIN_INT")
- value = "int.MinValue";
- if (cname == "M_INFINITY")
- value = "float.MaxValue";
- if (value == "M_MAX_UNSIGNED")
- value = "0xffffffff";
- // Input stuff
- if (module_->GetName() == "Input")
- {
- if (cname.StartsWith("KEY_"))
- {
- if (value.Length() == 1 && (IsAlpha(value[0]) || IsDigit(value[0])))
- value = "'" + value + "'";
- }
- // https://raw.githubusercontent.com/flibitijibibo/SDL2-CS/master/src/SDL2.cs
- if (value.StartsWith("SDL_BUTTON_") || value.StartsWith("SDL_HAT_"))
- {
- value = "(int) SDL." + value;
- }
- else if (value.StartsWith("SDLK_"))
- {
- value = "(int) SDL.SDL_Keycode." + value;
- }
- else if (value.StartsWith("SDL_SCANCODE_"))
- {
- value = "(int) SDL.SDL_Scancode." + value;
- }
- else if (value.StartsWith("SDL_CONTROLLER_BUTTON_"))
- {
- value = "(int) SDL.SDL_GameControllerButton." + value;
- }
- else if (value.StartsWith("SDL_CONTROLLER_AXIS_"))
- {
- value = "(int) SDL.SDL_GameControllerAxis." + value;
- }
- }
- String line = "public const " + managedType + " " + cname + " = " + value;
- if (managedType == "float" && !line.EndsWith("f") && IsDigit(line[line.Length()-1]))
- line += "f";
- line += ";\n";
- source += IndentLine(line);
- }
- Dedent();
- source += "\n";
- line = "}\n";
- source += IndentLine(line);
- }
- source += "\n";
- Dedent();
- }
- void CSModuleWriter::GenerateManagedModuleClass(String& sourceOut)
- {
- Indent();
- String source;
- String line = ToString("public static partial class %sModule\n", module_->GetName().CString());
- source += IndentLine(line);
- source += IndentLine("{\n");
- Indent();
- source += IndentLine("public static void Initialize()\n");
- source += IndentLine("{\n");
- Indent();
- Vector<SharedPtr<JSBClass>> classes = module_->classes_.Values();
- for (unsigned i = 0; i < classes.Size(); i++)
- {
- JSBClass* klass = classes.At(i);
- JSBPackage* package = module_->GetPackage();
- if (klass->IsNumberArray() || klass->IsAbstract())
- continue;
- const char* className = klass->GetName().CString();
- String classGuard = module_->GetClassDefineGuard(classes[i]->GetNativeName(), "csharp");
- if (classGuard.Length())
- {
- source += ToString("\n%s\n", classGuard.CString());
- }
- line = ToString("new NativeType(%s.csb_%s_%s_GetClassIDStatic (), ", className, package->GetName().CString(), className);
- line += ToString("typeof(%s), (IntPtr x) => { return new %s (x); } );\n",className, className);
- source += IndentLine(line);
- if (classGuard.Length())
- {
- source += ToString("#endif\n", classGuard.CString());
- }
- }
- Dedent();
- source += IndentLine("}\n");
- Dedent();
- source += IndentLine("}\n");
- sourceOut += source;
- Dedent();
- }
- void CSModuleWriter::GenerateManagedSource()
- {
- String source = "// Autogenerated";
- String moduleName = module_->GetPackage()->GetNamespace();
- source += "\nusing System;\nusing System.Collections.Generic;\nusing System.Runtime.InteropServices;\nusing static System.Reflection.IntrospectionExtensions;\n";
- if (moduleName == "Atomic")
- moduleName = "AtomicEngine";
- if (moduleName != "AtomicEngine")
- {
- source += "using AtomicEngine;\n";
- }
- source += "\n\n";
- source += "namespace " + moduleName + "\n";
- source += "{\n";
- GenerateManagedEnumsAndConstants(source);
- GenerateManagedModuleClass(source);
- GenerateManagedClasses(source);
- source += "}\n";
- JSBind* jsbind = module_->GetSubsystem<JSBind>();
- String filepath = jsbind->GetDestScriptFolder() + "/CSModule" + module_->name_ + ".cs";
- File file(module_->GetContext());
- file.Open(filepath, FILE_WRITE);
- file.Write(source.CString(), source.Length());
- file.Close();
- }
- void CSModuleWriter::GenerateSource()
- {
- GenerateNativeSource();
- GenerateManagedSource();
- }
- }
|