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

Implement remote scope option #107

Merged
merged 9 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions docs/.vitepress/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const beforeMount = (monaco: Monaco) => {

const Calls = ["SingleSync", "SingleAsync", "ManySync", "ManyAsync"] as const;

const Options = ["typescript", "write_checks", "casing", "server_output", "client_output", "manual_event_loop", "yield_type", "async_lib"] as const;
const Options = ["write_checks", "typescript", "typescript_max_tuple_length", "manual_event_loop", "remote_scope", "server_output", "client_output", "casing", "yield_type", "async_lib"] as const;
EncodedVenom marked this conversation as resolved.
Show resolved Hide resolved

const Casing = ["PascalCase", "camelCase", "snake_case"].map((value) => `"${value}"`);
const YieldType = ["yield", "future", "promise"].map((value) => `"${value}"`);
Expand Down Expand Up @@ -157,11 +157,13 @@ const beforeMount = (monaco: Monaco) => {
yield_type: YieldType,

typescript: Operators,
typescript_max_tuple_length: [],
write_checks: Operators,
manual_event_loop: Operators,

output_server: [],
output_client: [],
remote_scope: [],
server_output: [],
client_output: [],
async_lib: [],
} as const;

Expand Down
20 changes: 20 additions & 0 deletions docs/config/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ The paths are relative to the configuration file and should point to a lua(u) fi

<CodeBlock :code="outputExample" />

## `remote_scope`

This option changes the name of the remotes generated by Zap.

### Default

<CodeBlock code = 'opt remote_scope = "ZAP"' />


The generated remotes will be `ZAP_RELIABLE` and `ZAP_UNRELIABLE` respectively.


### Example

<CodeBlock code = 'opt remote_scope = "PACKAGE_NAME"' />


The generated remotes will change to be `PACKAGE_NAME_RELIABLE` and `PACKAGE_NAME_UNRELIABLE` respectively.


## `casing`

This option changes the casing of the API generated by Zap.
Expand Down
2 changes: 2 additions & 0 deletions zap/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub struct Config<'src> {
pub typescript_max_tuple_length: f64,
pub manual_event_loop: bool,

pub remote_scope: &'src str,

pub server_output: &'src str,
pub client_output: &'src str,

Expand Down
11 changes: 0 additions & 11 deletions zap/src/output/luau/client.luau

This file was deleted.

22 changes: 21 additions & 1 deletion zap/src/output/luau/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ impl<'src> ClientOutput<'src> {
self.push_line(&format!("local function {send_events}()"));
self.indent();
} else {
self.push_line("local time = 0");
self.push_line("RunService.Heartbeat:Connect(function(dt)");
self.indent();
self.push_line("time += dt");
Expand Down Expand Up @@ -828,6 +829,23 @@ impl<'src> ClientOutput<'src> {
self.push_line("return returns");
}

pub fn push_remotes(&mut self) {
self.push_line(&format!("local reliable = ReplicatedStorage:WaitForChild(\"{}_RELIABLE\")", self.config.remote_scope));
self.push_line(&format!("local unreliable = ReplicatedStorage:WaitForChild(\"{}_UNRELIABLE\")", self.config.remote_scope));
self.push("\n");
self.push_line(&format!("assert(reliable:IsA(\"RemoteEvent\"), \"Expected {}_RELIABLE to be a RemoteEvent\")", self.config.remote_scope));
self.push_line(&format!("assert(unreliable:IsA(\"UnreliableRemoteEvent\"), \"Expected {}_UNRELIABLE to be an UnreliableRemoteEvent\")", self.config.remote_scope));
self.push("\n");
}

pub fn push_check_server(&mut self) {
self.push_line("if RunService:IsServer() then");
self.indent();
self.push_line("error(\"Cannot use the client module on the server!\")");
self.dedent();
self.push_line("end");
}

pub fn output(mut self) -> String {
self.push_file_header("Client");

Expand All @@ -839,7 +857,9 @@ impl<'src> ClientOutput<'src> {

self.push_studio();

self.push(include_str!("client.luau"));
self.push_check_server();

self.push_remotes();

self.push_tydecls();

Expand Down
33 changes: 0 additions & 33 deletions zap/src/output/luau/server.luau

This file was deleted.

63 changes: 62 additions & 1 deletion zap/src/output/luau/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,63 @@ impl<'a> ServerOutput<'a> {
self.push_line("return returns");
}

pub fn push_check_client(&mut self) {
self.push_line("local Players = game:GetService(\"Players\")");
self.push("\n");
self.push_line("if RunService:IsClient() then");
self.indent();
self.push_line("error(\"Cannot use the server module on the client!\")");
self.dedent();
self.push_line("end");
}

pub fn push_create_remotes(&mut self) {
self.push_line(&format!("local reliable = ReplicatedStorage:FindFirstChild(\"{}_RELIABLE\")", self.config.remote_scope));
self.push_line("if reliable == nil then");
self.indent();
self.push_line("reliable = Instance.new(\"RemoteEvent\")");
self.push_line(&format!("reliable.Name = \"{}\"", self.config.remote_scope));
self.push_line("reliable.Parent = ReplicatedStorage");
self.dedent();
self.push_line("end");

self.push("\n");

self.push_line(&format!("local unreliable = ReplicatedStorage:FindFirstChild(\"{}_UNRELIABLE\")", self.config.remote_scope));
self.push_line("if unreliable == nil then");
self.indent();
self.push_line("unreliable = Instance.new(\"UnreliableRemoteEvent\")");
self.push_line(&format!("unreliable.Name = \"{}\"", self.config.remote_scope));
self.push_line("unreliable.Parent = ReplicatedStorage");
self.dedent();
self.push_line("end");
}

pub fn push_player_map(&mut self) {
self.push_line("local player_map = {}");
self.push("\n");
self.push_line("local function load_player(player: Player)");
self.indent();
self.push_line("if player_map[player] then");
self.indent();
self.push_line("load(player_map[player])");
self.dedent();
self.push_line("else");
self.indent();
self.push_line("load_empty()");
self.dedent();
self.push_line("end");
self.dedent();
self.push_line("end");
self.push("\n");
self.push_line("Players.PlayerRemoving:Connect(function(player)");
self.indent();
self.push_line("player_map[player] = nil");
self.dedent();
self.push_line("end");
self.push("\n");
}

pub fn output(mut self) -> String {
self.push_file_header("Server");

Expand All @@ -888,7 +945,11 @@ impl<'a> ServerOutput<'a> {

self.push_studio();

self.push(include_str!("server.luau"));
self.push_check_client();

self.push_create_remotes();

self.push_player_map();

self.push_tydecls();

Expand Down
3 changes: 3 additions & 0 deletions zap/src/parser/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ impl<'src> Converter<'src> {

let (server_output, ..) = self.str_opt("server_output", "network/server.lua", &config.opts);
let (client_output, ..) = self.str_opt("client_output", "network/client.lua", &config.opts);
let (remote_scope, ..) = self.str_opt("remote_scope", "ZAP", &config.opts);
EncodedVenom marked this conversation as resolved.
Show resolved Hide resolved

let casing = self.casing_opt(&config.opts);
let yield_type = self.yield_type_opt(typescript, &config.opts);
Expand All @@ -107,6 +108,8 @@ impl<'src> Converter<'src> {
typescript_max_tuple_length,
manual_event_loop,

remote_scope,

server_output,
client_output,

Expand Down