Skip to content

Commit ada9c42

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/Add-ccEvents' into Add-ccEvents
2 parents 88cff38 + 5ca9f71 commit ada9c42

File tree

6 files changed

+174
-125
lines changed

6 files changed

+174
-125
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This emulates the basic ccTweaked functions missing in basic-lua.
77
- vector-functions
88
- settings
99
- (no validations what so ever as of yet)
10+
- os.pullEvent etc.
1011

1112
Additionally it emulates our **[scm](https://github.com/mc-cc-scripts/script-manager)** script and includes the **[json](https://gist.github.com/tylerneylon/59f4bcf316be525b30ab)** handler - which makes tests a lot easier.
1213

@@ -34,6 +35,10 @@ local scriptToTest = require("scriptToTest")
3435
-- without any need to modify the script you want to test
3536
```
3637

38+
#### os
39+
[how to test with os functions](docs/ccOS.md)
40+
41+
3742
### How to import these scripts
3843

3944
For how to import the scipts, an example is already used by this repo for some of ITS dependancies (which you will also need):

events.lua renamed to ccOS.lua

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ local class = require("ccClass")
2828
---@field waiting boolean To confirm if a function was actually called or is just wrapped.
2929
-- Relevant for Events.run, as it resumes ALL coroutines - therefore invoking each method if not for this check
3030

31-
---@class ccEvent : subThread
31+
---@class ccOS : subThread
3232
---@field FIFOEventList Event[]
3333
---@field TimerList timerList
3434
---@field subThreads table<string, subThread>
3535
---@field time number eq. os.time("ingame") from ccTweaked)
3636
---@field epoch number eq. os.epoch("ingame") from ccTweaked)
37-
local Events = class(
37+
local ccOS = class(
3838
function(baseClass)
39-
---@cast baseClass ccEvent
39+
---@cast baseClass ccOS
4040
baseClass.FIFOEventList = {}
4141
baseClass.TimerList = {timers = {}, currentID = 1}
4242
baseClass.time = 0
@@ -48,7 +48,7 @@ local Events = class(
4848
---comment
4949
---@param threadHolder subThread
5050
---@param ... any
51-
function Events:resumeThread(threadHolder, ...)
51+
function ccOS:resumeThread(threadHolder, ...)
5252
local ok
5353
local wrapper
5454
local result
@@ -77,9 +77,9 @@ end
7777
---@param wrapModule? boolean This will modify the Module!
7878
---@param ... any If loading a Module, these are the parameters
7979
---@return T|nil result if module is wrapped, it returns the wrapped module.
80-
function Events:wrap(func, wrapModule, ...)
80+
function ccOS:wrap(func, wrapModule, ...)
8181
assert(self.subThreads, "Do not use Eventclass, create an Event-Object via 'local eventObj = ccEvent()'")
82-
local manager = self
82+
local ccOSInstance = self
8383
local env = {}
8484
---@class EventOS: oslib
8585
env.os = setmetatable({
@@ -95,15 +95,31 @@ function Events:wrap(func, wrapModule, ...)
9595
return table.unpack(event)
9696
end,
9797
queueEvent = function(name, ...)
98-
manager:invoke(name, ...)
98+
ccOSInstance:invoke(name, ...)
9999
end,
100100
startTimer = function(time)
101-
assert(type(time) == "number")
102-
return manager:addTimer(time)
101+
assert(type(time) == "number" ,type(time))
102+
return ccOSInstance:addTimer(time)
103103
end,
104104
cancleTimer = function(id)
105-
manager:removeTimer(id)
106-
end
105+
ccOSInstance:removeTimer(id)
106+
end,
107+
sleep = function(time)
108+
local expectedId = env.os.startTimer(time)
109+
local correctTimer = false
110+
while not correctTimer do
111+
local _, id = env.os.pullEvent("timer")
112+
correctTimer = id == expectedId
113+
end
114+
end,
115+
time = function()
116+
-- TODO: add locale
117+
return ccOSInstance.time
118+
end,
119+
epoch = function()
120+
-- TODO: add args
121+
return ccOSInstance.epoch
122+
end,
107123

108124
}, {__index = os})
109125
setmetatable(env, {__index = _G})
@@ -167,7 +183,7 @@ end
167183

168184
---@param time number seconds
169185
---@return number timerID
170-
function Events:addTimer(time)
186+
function ccOS:addTimer(time)
171187
assert(type(time) == "number" and time > 0)
172188
local triggerAfter = time * 1000 + self.epoch - 1
173189
local id = self.TimerList.currentID
@@ -176,19 +192,19 @@ function Events:addTimer(time)
176192
return id
177193
end
178194

179-
function Events:removeTimer(id)
195+
function ccOS:removeTimer(id)
180196
assert(type(id) == "number")
181197
for k,v in pairs(self.TimerList.timers) do
182198
if v.id == id then
183-
self.TimerList.currentID[id] = nil
199+
self.TimerList.timers[id] = nil
184200
end
185201
end
186202
end
187203

188204
---Passes time (in Seconds)
189205
---Required for timers
190206
---@param time number seconds
191-
function Events:passTime(time)
207+
function ccOS:passTime(time)
192208
assert(type(time) == "number")
193209
time = time * 1000
194210
self.time = (self.time + (time / 60 / 24)) % 24
@@ -203,14 +219,14 @@ function Events:passTime(time)
203219

204220
end
205221

206-
function Events:invoke(eventName, ...)
222+
function ccOS:invoke(eventName, ...)
207223
---@type Event
208224
local event = {eventName = eventName, receivedBy = {}, eventArgs = {...}}
209225
table.insert(self.FIFOEventList, event)
210226
self:checkForUpdates()
211227
end
212228

213-
function Events:checkForUpdates()
229+
function ccOS:checkForUpdates()
214230
local modifier = 0
215231

216232
local removeEvent = function(i)
@@ -243,4 +259,4 @@ function Events:checkForUpdates()
243259
end
244260

245261

246-
return Events
262+
return ccOS

docs/ccOS.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# OS
2+
3+
Restrictions:
4+
---
5+
6+
__YOU CANNOT USE EVENTS GLOBALLY.__
7+
8+
_You cannot yield the "main" coroutine, therefore os.pullevent() cannot just be set globally.
9+
Therefore You need to wrap everything you intent to run in the wrapper._
10+
11+
___Modules CANNOT yield outside of their fields__
12+
13+
```lua
14+
local TestModule = {}
15+
-- ...
16+
coroutine.yield() -- <- Breaks the EventManager, as the module cannot be wrapped
17+
-- ...
18+
return TestModule
19+
```
20+
21+
22+
## How to use
23+
24+
### for functions
25+
```lua
26+
-- import
27+
local osEmulator = require("ccOS")
28+
29+
-- create instance
30+
local osInstance = osEmulator()
31+
32+
local testFunction = osInstance:wrap(function()
33+
os.pullEvent("CustomEvent")
34+
print("Event got called")
35+
end)
36+
37+
testFunction() -- < Programm yields until event gets invoked, in the instances' thread
38+
osInstance:invoke("CustomEvent") -- < Programm continues -> prints: "Event got called"
39+
40+
```
41+
42+
### for Modules
43+
```lua
44+
local osEmulator = require("ccOS")
45+
-- create instance
46+
local osInstance = osEmulator()
47+
48+
local moduleFile = loadfile("pathToFile","t")
49+
local testModule = osInstance:wrap(moduleFile, true, ...)
50+
-- <true> tells the wrapper to wrap all functioncalls, so they run in a coroutine thread, instead of the callers thread
51+
-- <...> as the module will need to be called once, you can provide additional parameters as need
52+
53+
testModule:someFunction(...)
54+
-- the function will now run on the osInstance-Thread, knowing all currently implemented os-Functions
55+
```

docs/events.md

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)