diff options
Diffstat (limited to '')
-rw-r--r-- | src/universe.c | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/universe.c b/src/universe.c new file mode 100644 index 0000000..ba78396 --- /dev/null +++ b/src/universe.c | |||
@@ -0,0 +1,74 @@ | |||
1 | /* | ||
2 | * UNIVERSE.C Copyright (c) 2017, Benoit Germain | ||
3 | */ | ||
4 | |||
5 | /* | ||
6 | =============================================================================== | ||
7 | |||
8 | Copyright (C) 2017 Benoit Germain <bnt.germain@gmail.com> | ||
9 | |||
10 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
11 | of this software and associated documentation files (the "Software"), to deal | ||
12 | in the Software without restriction, including without limitation the rights | ||
13 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
14 | copies of the Software, and to permit persons to whom the Software is | ||
15 | furnished to do so, subject to the following conditions: | ||
16 | |||
17 | The above copyright notice and this permission notice shall be included in | ||
18 | all copies or substantial portions of the Software. | ||
19 | |||
20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
21 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
22 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
23 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
24 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
25 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
26 | THE SOFTWARE. | ||
27 | |||
28 | =============================================================================== | ||
29 | */ | ||
30 | |||
31 | #include "compat.h" | ||
32 | #include "macros_and_utils.h" | ||
33 | #include "universe.h" | ||
34 | |||
35 | // crc64/we of string "UNIVERSE_REGKEY" generated at https://www.nitrxgen.net/hashgen/ | ||
36 | static void* const UNIVERSE_REGKEY = ((void*)0x9f877b2cf078f17f); | ||
37 | |||
38 | // ################################################################################################ | ||
39 | |||
40 | struct s_Universe* universe_create( lua_State* L) | ||
41 | { | ||
42 | struct s_Universe* U = (struct s_Universe*) lua_newuserdata( L, sizeof(struct s_Universe)); // universe | ||
43 | memset( U, 0, sizeof( struct s_Universe)); | ||
44 | lua_pushlightuserdata( L, UNIVERSE_REGKEY); // universe UNIVERSE_REGKEY | ||
45 | lua_pushvalue( L, -2); // universe UNIVERSE_REGKEY universe | ||
46 | lua_rawset( L, LUA_REGISTRYINDEX); // universe | ||
47 | return U; | ||
48 | } | ||
49 | |||
50 | // ################################################################################################ | ||
51 | |||
52 | void universe_store( lua_State* L, struct s_Universe* U) | ||
53 | { | ||
54 | STACK_CHECK( L); | ||
55 | lua_pushlightuserdata( L, UNIVERSE_REGKEY); | ||
56 | lua_pushlightuserdata( L, U); | ||
57 | lua_rawset( L, LUA_REGISTRYINDEX); | ||
58 | STACK_END( L, 0); | ||
59 | } | ||
60 | |||
61 | // ################################################################################################ | ||
62 | |||
63 | struct s_Universe* universe_get( lua_State* L) | ||
64 | { | ||
65 | struct s_Universe* universe; | ||
66 | STACK_GROW( L, 2); | ||
67 | STACK_CHECK( L); | ||
68 | lua_pushlightuserdata( L, UNIVERSE_REGKEY); | ||
69 | lua_rawget( L, LUA_REGISTRYINDEX); | ||
70 | universe = lua_touserdata( L, -1); // NULL if nil | ||
71 | lua_pop( L, 1); | ||
72 | STACK_END( L, 0); | ||
73 | return universe; | ||
74 | } | ||