Skip to content

Commit

Permalink
Explicit script namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
cvet committed Dec 12, 2024
1 parent 5a92248 commit 8f4b9fe
Show file tree
Hide file tree
Showing 16 changed files with 90 additions and 68 deletions.
4 changes: 3 additions & 1 deletion BuildTools/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2678,7 +2678,8 @@ def writeNames(name, lst):
writeFile(' hstring ' + i + ' = hstring("' + i + '");')
writeFile('}')
writeFile('')
writeFile('// FOS Common')
writeFile('namespace Content')
writeFile('{')
writeFile('')
if '.fodlg' in content:
writeNames('Dialog', content['.fodlg'])
Expand All @@ -2687,6 +2688,7 @@ def writeNames(name, lst):
for k in sorted(content.keys()):
if k.startswith('Proto'):
writeNames(k[5:], content[k])
writeFile('}')

except Exception as ex:
showError('Can\'t generate scripts', ex)
Expand Down
2 changes: 1 addition & 1 deletion Source/Scripting/AngelScript/.clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ LambdaBodyIndentation: Signature
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
NamespaceIndentation: Inner
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 4
ObjCBreakBeforeNestedBlockParam: true
Expand Down
5 changes: 4 additions & 1 deletion Source/Scripting/AngelScript/ClientCore.fos
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// FOS Client Mapper Sort -9
namespace ClientCore // Sort -9
{

// Messages types in message boxes
#define FOMB_GAME (0)
Expand Down Expand Up @@ -41,3 +42,5 @@
#define DRAW_PRIMITIVE_LINESTRIP (2)
#define DRAW_PRIMITIVE_TRIANGLELIST (3)
#define DRAW_PRIMITIVE_TRIANGLESTRIP (4)

}
5 changes: 4 additions & 1 deletion Source/Scripting/AngelScript/Colors.fos
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// FOS Common Sort -6
namespace Colors // Sort -6
{

#define COLOR_NEUTRAL ucolor(128, 128, 128)
#define COLOR_TEXT ucolor(80, 255, 0) // Default text color
Expand Down Expand Up @@ -27,3 +28,5 @@
#define COLOR_GREEN_RED ucolor(20, 220, 220)
#define COLOR_SAND ucolor(163, 131, 20)
#define COLOR_LEMON ucolor(255, 255, 20)

}
5 changes: 4 additions & 1 deletion Source/Scripting/AngelScript/Core.fos
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// FOS Common Sort -10
namespace Core // Sort -10
{

// Say types
#define SAY_NORM (1)
Expand Down Expand Up @@ -108,3 +109,5 @@
#define LIGHT_DISABLE_DIR #(dir)(1 << Math::Clamp(dir, 0, 5))
#define LIGHT_GLOBAL (0x40)
#define LIGHT_INVERSE (0x80)

}
9 changes: 8 additions & 1 deletion Source/Scripting/AngelScript/FixedDropMenu.fos
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// FOS Client
namespace FixedDropMenu
{

#if CLIENT

funcdef bool DropMenuNeedShowButton(Critter cr, Item item);
funcdef void DropMenuButtonAction(Critter cr, Item item);
Expand Down Expand Up @@ -200,3 +203,7 @@ class FixedDropMenu : Gui::IDropMenu
}
}
};

#endif

}
9 changes: 8 additions & 1 deletion Source/Scripting/AngelScript/Gui.fos
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// FOS Client
namespace Gui
{

#if CLIENT

///@ Enum CursorType Default = 0

Expand Down Expand Up @@ -4020,3 +4023,7 @@ uint GetScreenCount(int screenIndex)
}
return count;
}

#endif

}
9 changes: 8 additions & 1 deletion Source/Scripting/AngelScript/Input.fos
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// FOS Client
namespace Input
{

#if CLIENT

funcdef bool KeyDownCallback(KeyCode key, string text);
funcdef void KeyUpCallback(KeyCode key);
Expand Down Expand Up @@ -156,3 +159,7 @@ void OnInputLost() // Engine callback
// Gui:: handler
Gui::EngineCallback_InputLost();
}

#endif

}
13 changes: 12 additions & 1 deletion Source/Scripting/AngelScript/LineTracer.fos
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// FOS Server
namespace LineTracer
{

// Author: Atom

// utilities for tracing
Expand All @@ -21,6 +23,7 @@ float GetDirectionF(uint16 hx, uint16 hy, uint16 tx, uint16 ty)
{
float nx = 3 * (float(tx) - float(hx));
float ny = SQRT3T2_FLOAT * (float(ty) - float(hy)) - (float(tx % 2) - float(hx % 2)) * SQRT3_FLOAT;

return 180.0f + RAD2DEG * math::atan2(ny, nx); // In degrees, because cvet loves the degrees
}

Expand All @@ -34,6 +37,7 @@ uint LineTracerHex(uint16 hx, uint16 hy, uint16 tx, uint16 ty, Map map, uint dis

float dir = GetDirectionF(hx, hy, tx, ty);
uint8 dir1, dir2;

if ((30.0f <= dir) && (dir < 90.0f)) {
dir1 = 5;
dir2 = 0;
Expand Down Expand Up @@ -85,6 +89,7 @@ uint LineTracerHex(uint16 hx, uint16 hy, uint16 tx, uint16 ty, Map map, uint dis
t2y = cy;
map.MoveHexByDir(t1x, t1y, dir1, 1);
map.MoveHexByDir(t2x, t2y, dir2, 1);

c1x = 3 * float(t1x);
c1y = SQRT3T2_FLOAT * float(t1y) - (float(t1x % 2)) * SQRT3_FLOAT;
c2x = 3 * float(t2x);
Expand All @@ -93,6 +98,7 @@ uint LineTracerHex(uint16 hx, uint16 hy, uint16 tx, uint16 ty, Map map, uint dis
dist2 = dx * (y1 - c2y) - dy * (x1 - c2x);
dist1 = ((dist1 > 0) ? dist1 : -dist1);
dist2 = ((dist2 > 0) ? dist2 : -dist2);

if (dist1 <= dist2) {
cx = t1x;
cy = t1y;
Expand Down Expand Up @@ -128,6 +134,7 @@ uint LineTracerSquare(uint16 hx, uint16 hy, uint16 tx, uint16 ty, Map map, uint
float dir = math::atan2(float(ty - hy), float(tx - hx));
float dx = math::cos(dir);
float dy = math::sin(dir);

if (math::abs(dx) > math::abs(dy)) {
dy /= math::abs(dx);
dx = (dx > 0 ? 1.0f : -1.0f);
Expand All @@ -136,6 +143,7 @@ uint LineTracerSquare(uint16 hx, uint16 hy, uint16 tx, uint16 ty, Map map, uint
dx /= math::abs(dy);
dy = (dy > 0 ? 1.0f : -1.0f);
}

float x1 = float(hx) + 0.5f;
float y1 = float(hy) + 0.5f;
uint16 maxx = map.Width;
Expand All @@ -147,6 +155,7 @@ uint LineTracerSquare(uint16 hx, uint16 hy, uint16 tx, uint16 ty, Map map, uint
y1 += dy;
cx = uint16(math::floor(x1));
cy = uint16(math::floor(y1));

if (cx >= maxx || cy >= maxy) {
dist = i - 1;
break;
Expand All @@ -160,3 +169,5 @@ uint LineTracerSquare(uint16 hx, uint16 hy, uint16 tx, uint16 ty, Map map, uint
context.FinishExec(map, dist);
return dist;
}

}
31 changes: 3 additions & 28 deletions Source/Scripting/AngelScript/MapperCore.fos
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// FOS Mapper Sort -8
namespace MapperCore // Sort -8
{

// Tabs
#define TAB_CUSTOM0 (0)
Expand All @@ -20,35 +21,9 @@
#define TAB_MESAGE_BOX (16) // Only for Game.TabSetName(...)
#define TAB_MAPS (17) // Only for Game.TabSetName(...)

// Fonts
#define FONT_DEFAULT (0)

// Font flags
#define FT_NOBREAK (0x0001)
#define FT_NOBREAK_LINE (0x0002)
#define FT_CENTERX (0x0004)
#define FT_CENTERY (0x0008)
#define FT_CENTERR (0x0010)
#define FT_BOTTOM (0x0020)
#define FT_UPPER (0x0040)
#define FT_NO_COLORIZE (0x0080)
#define FT_ALIGN (0x0100)
#define FT_SKIPLINES(l) (0x0200 | ((l) << 16))
#define FT_BORDERED (0x0400)

// Cursors
#define CURSOR_DEFAULT (0)
#define CURSOR_MOVE_SELECTION (1)
#define CURSOR_PLACE_OBJECT (2)

// Draw primitive types
#define DRAW_PRIMITIVE_POINTLIST (0)
#define DRAW_PRIMITIVE_LINELIST (1)
#define DRAW_PRIMITIVE_LINESTRIP (2)
#define DRAW_PRIMITIVE_TRIANGLELIST (3)
#define DRAW_PRIMITIVE_TRIANGLESTRIP (4)

// Sprite cut type
#define SPRITE_CUT_NONE (0)
#define SPRITE_CUT_HORIZONTAL (1)
#define SPRITE_CUT_VERTICAL (2)
}
5 changes: 4 additions & 1 deletion Source/Scripting/AngelScript/Math.fos
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// FOS Common
namespace Math
{

int Pow2(int x)
{
Expand Down Expand Up @@ -102,3 +103,5 @@ int Fib(int n)

return y;
}

}
6 changes: 5 additions & 1 deletion Source/Scripting/AngelScript/Serializator.fos
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// FOS Common
namespace Serializator
{

// Data serialization

class Serializator
Expand Down Expand Up @@ -756,3 +758,5 @@ class Serializator
return this;
}
};

}
9 changes: 8 additions & 1 deletion Source/Scripting/AngelScript/Sprite.fos
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// FOS Client Mapper
namespace Sprite
{

///@ Enum SpriteLayout None
///@ Enum SpriteLayout Tile
///@ Enum SpriteLayout Center
///@ Enum SpriteLayout Stretch
///@ Enum SpriteLayout Zoom

#if CLIENT

class Sprite
{
Sprite()
Expand Down Expand Up @@ -175,3 +178,7 @@ class Sprite
hstring LoadedName;
ucolor Color = COLOR_NEUTRAL;
};

#endif

}
6 changes: 5 additions & 1 deletion Source/Scripting/AngelScript/Time.fos
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// FOS Common Sort -5
namespace Time
{

// Some stuff for work with time

uint RealMs(uint value)
Expand Down Expand Up @@ -115,3 +117,5 @@ bool IsTimeoutActive(uint timeout)
{
return timeout > Game.GetFullSecond();
}

}
5 changes: 4 additions & 1 deletion Source/Scripting/AngelScript/Tween.fos
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// FOS Common
namespace Tween
{

class Tween
{
Expand Down Expand Up @@ -81,3 +82,5 @@ class Tween
private
uint startTick;
};

}
35 changes: 9 additions & 26 deletions Source/Scripting/AngelScriptScripting-Template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4834,30 +4834,10 @@ static void CompileRootModule(asIScriptEngine* engine, const FileSystem& resourc

const auto line_sep = script_content.find('\n');
const auto first_line = script_content.substr(0, line_sep);
if (first_line.find("// FOS ") == string::npos) {
throw ScriptCompilerException("No FOS header in script file", script_name);
}

#if SERVER_SCRIPTING
if (first_line.find("Common") == string::npos && first_line.find("Server") == string::npos) {
continue;
}
#elif CLIENT_SCRIPTING
if (first_line.find("Common") == string::npos && first_line.find("Client") == string::npos) {
continue;
}
#elif SINGLE_SCRIPTING
if (first_line.find("Common") == string::npos && first_line.find("Single") == string::npos) {
continue;
}
#elif MAPPER_SCRIPTING
if (first_line.find("Common") == string::npos && first_line.find("Mapper") == string::npos) {
continue;
}
#endif

int sort = 0;
const auto sort_pos = first_line.find("Sort ");

if (sort_pos != string::npos) {
sort = strex(first_line.substr(sort_pos + "Sort "_len)).substringUntil(' ').toInt();
}
Expand All @@ -4866,7 +4846,7 @@ static void CompileRootModule(asIScriptEngine* engine, const FileSystem& resourc
final_script_files.emplace(script_path, std::move(script_content));
}

std::sort(final_script_files_order.begin(), final_script_files_order.end(), [](auto& a, auto& b) {
std::stable_sort(final_script_files_order.begin(), final_script_files_order.end(), [](auto& a, auto& b) {
if (std::get<0>(a) == std::get<0>(b)) {
return std::get<1>(a) < std::get<1>(b);
}
Expand All @@ -4879,11 +4859,9 @@ static void CompileRootModule(asIScriptEngine* engine, const FileSystem& resourc
root_script.reserve(final_script_files.size() * 128);

for (auto&& [script_order, script_name, script_path] : final_script_files_order) {
root_script.append("namespace ");
root_script.append(script_name);
root_script.append(" {\n#include \"");
root_script.append("#include \"");
root_script.append(script_path);
root_script.append("\"\n}\n");
root_script.append("\"\n");
}

Preprocessor::UndefAll();
Expand Down Expand Up @@ -4919,23 +4897,27 @@ static void CompileRootModule(asIScriptEngine* engine, const FileSystem& resourc
}

asIScriptModule* mod = engine->GetModule("Root", asGM_ALWAYS_CREATE);

if (mod == nullptr) {
throw ScriptCompilerException("Create root module failed");
}

int as_result = mod->AddScriptSection("Root", result.String.c_str());

if (as_result < 0) {
throw ScriptCompilerException("Unable to add script section", as_result);
}

as_result = mod->Build();

if (as_result < 0) {
throw ScriptCompilerException("Unable to build module", as_result);
}

vector<asBYTE> buf;
BinaryStream binary {buf};
as_result = mod->SaveByteCode(&binary);

if (as_result < 0) {
throw ScriptCompilerException("Unable to save byte code", as_result);
}
Expand All @@ -4962,6 +4944,7 @@ static void CompileRootModule(asIScriptEngine* engine, const FileSystem& resourc
#endif

auto file = DiskFileSystem::OpenFile(script_out_path, true);

if (!file) {
throw ScriptCompilerException("Can't write binary to file", script_out_path);
}
Expand Down

0 comments on commit 8f4b9fe

Please sign in to comment.