Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add manual event loop option #47

Merged
merged 8 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions zap/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Config<'src> {

pub write_checks: bool,
pub typescript: bool,
pub manual_event_loop: bool,

pub server_output: Option<&'src str>,
pub client_output: Option<&'src str>,
Expand Down
20 changes: 0 additions & 20 deletions zap/src/output/luau/client.luau
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,3 @@ assert(unreliable:IsA("UnreliableRemoteEvent"), "Expected ZAP_UNRELIABLE to be a
local event_queue: { [number]: { any } } = {}

local time = 0

RunService.Heartbeat:Connect(function(dt)
time += dt

if time >= (1 / 61) then
time -= (1 / 61)

if outgoing_used ~= 0 then
local buff = buffer.create(outgoing_used)
buffer.copy(buff, 0, outgoing_buff, 0, outgoing_used)

reliable:FireServer(buff, outgoing_inst)

outgoing_buff = buffer.create(64)
outgoing_used = 0
outgoing_size = 64
table.clear(outgoing_inst)
end
end
end)
54 changes: 54 additions & 0 deletions zap/src/output/luau/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,52 @@ impl<'src> ClientOutput<'src> {
}
}

fn push_event_loop(&mut self) {
self.push_line("");
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved

let delta_time = if self.config.manual_event_loop {
self.config.casing.with("DeltaTime", "deltaTime", "delta_time")
} else {
"dt"
};
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved

if self.config.manual_event_loop {
let send_events = self.config.casing.with("SendEvents", "sendEvents", "send_events");
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved

self.push_line(&format!("local function {send_events}({delta_time}: number)"));
} else {
self.push_line(&format!("RunService.Heartbeat:Connect(function({delta_time})"));
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved
}

self.push_line(&format!(
r#" time += {delta_time}

if time >= 1 / 61 then
time -= 1 / 61

if outgoing_used ~= 0 then
local buff = buffer.create(outgoing_used)
buffer.copy(buff, 0, outgoing_buff, 0, outgoing_used)

reliable:FireServer(buff, outgoing_inst)

outgoing_buff = buffer.create(64)
outgoing_used = 0
outgoing_size = 64
table.clear(outgoing_inst)
end
end"#,
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved
));

if self.config.manual_event_loop {
self.push_line("end");
} else {
self.push_line("end)");
}
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved

self.push_line("");
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved
}

fn push_reliable_header(&mut self) {
self.push_line("reliable.OnClientEvent:Connect(function(buff, inst)");
self.indent();
Expand Down Expand Up @@ -476,6 +522,12 @@ impl<'src> ClientOutput<'src> {
self.push_line("return {");
self.indent();

if self.config.manual_event_loop {
let send_events = self.config.casing.with("SendEvents", "sendEvents", "send_events");

self.push_line(&format!("{send_events} = {send_events},"));
}

self.push_return_outgoing();
self.push_return_listen();

Expand All @@ -495,6 +547,8 @@ impl<'src> ClientOutput<'src> {

self.push_tydecls();

self.push_event_loop();

self.push_callback_lists();

if self
Expand Down
16 changes: 0 additions & 16 deletions zap/src/output/luau/server.luau
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,3 @@ end
Players.PlayerRemoving:Connect(function(player)
player_map[player] = nil
end)

RunService.Heartbeat:Connect(function()
for player, outgoing in player_map do
if outgoing.used > 0 then
local buff = buffer.create(outgoing.used)
buffer.copy(buff, 0, outgoing.buff, 0, outgoing.used)

reliable:FireClient(player, buff, outgoing.inst)

outgoing.buff = buffer.create(64)
outgoing.used = 0
outgoing.size = 64
table.clear(outgoing.inst)
end
end
end)
44 changes: 44 additions & 0 deletions zap/src/output/luau/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,42 @@ impl<'a> ServerOutput<'a> {
}
}

fn push_event_loop(&mut self) {
self.push_line("");
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved

if self.config.manual_event_loop {
let send_events = self.config.casing.with("SendEvents", "sendEvents", "send_events");

self.push_line(&format!("local function {send_events}()"));
} else {
self.push_line("RunService.Heartbeat:Connect(function()");
}

self.push_line(
r#" for player, outgoing in player_map do
if outgoing.used > 0 then
local buff = buffer.create(outgoing.used)
buffer.copy(buff, 0, outgoing.buff, 0, outgoing.used)

reliable:FireClient(player, buff, outgoing.inst)

outgoing.buff = buffer.create(64)
outgoing.used = 0
outgoing.size = 64
table.clear(outgoing.inst)
end
end"#,
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved
);

if self.config.manual_event_loop {
self.push_line("end");
} else {
self.push_line("end)");
}
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved

self.push_line("");
sasial-dev marked this conversation as resolved.
Show resolved Hide resolved
}

fn push_reliable_header(&mut self) {
self.push_line("reliable.OnServerEvent:Connect(function(player, buff, inst)");
self.indent();
Expand Down Expand Up @@ -539,6 +575,12 @@ impl<'a> ServerOutput<'a> {
self.push_line("return {");
self.indent();

if self.config.manual_event_loop {
let send_events = self.config.casing.with("SendEvents", "sendEvents", "send_events");

self.push_line(&format!("{send_events} = {send_events},"));
}

self.push_return_outgoing();
self.push_return_listen();

Expand All @@ -558,6 +600,8 @@ impl<'a> ServerOutput<'a> {

self.push_tydecls();

self.push_event_loop();

self.push_callback_lists();

if self
Expand Down
11 changes: 11 additions & 0 deletions zap/src/parser/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl<'src> SyntaxConfig<'src> {
fn into_config(self, state: &mut ConvertState<'src>) -> Config<'src> {
let mut write_checks = false;
let mut typescript = false;
let mut manual_event_loop = false;

let mut server_output = None;
let mut client_output = None;
Expand All @@ -85,6 +86,15 @@ impl<'src> SyntaxConfig<'src> {
}),
},

"manual_event_loop" => match opt.value.kind {
SyntaxOptValueKind::Bool(value) => manual_event_loop = value.into_config(),

_ => state.push_report(Report::AnalyzeInvalidOptValue {
span: opt.value.span(),
expected: "boolean",
}),
},

"server_output" => match opt.value.kind {
SyntaxOptValueKind::Str(value) => server_output = Some(value.into_config()),

Expand Down Expand Up @@ -141,6 +151,7 @@ impl<'src> SyntaxConfig<'src> {

write_checks,
typescript,
manual_event_loop,

server_output,
client_output,
Expand Down
Loading