Skip to content

Commit a0840ab

Browse files
authored
fix: freeze global constants (#554)
* deps * url * onEvent * bump * freeze * u16 * ChatMessage * deps
1 parent 9725b1c commit a0840ab

22 files changed

Lines changed: 97 additions & 59 deletions

File tree

Cargo.lock

Lines changed: 22 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ members = [
2323

2424
[workspace.package]
2525
description = "Multiplayer strategy game"
26-
version = "0.7.19"
26+
version = "0.7.20"
2727
edition = "2024"
2828
rust-version = "1.99"
2929
license = "AGPL-3.0-only"

app/src-tauri/tauri.conf.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://schema.tauri.app/config/2.0.0",
33
"productName": "Call of Nil",
4-
"version": "0.7.19",
4+
"version": "0.7.20",
55
"identifier": "tsukilabs.nil",
66
"build": {
77
"beforeDevCommand": "pnpm run -F app dev",

app/src/App.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import type { Locale } from "@tsukilabs/nil-bindings";
1616
import { setDragDropEventListener } from "@/lib/event";
1717
import { createTrayIcon, showWindow } from "@/commands";
1818
import { onAltKeyDown, onKeyDown, useBreakpoints } from "@tb-dev/vue";
19-
import { defineGlobalCheats, defineGlobalCommands } from "@/lib/global";
2019
import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow";
2120
import { type BasicColorSchema, useColorMode, watchImmediate } from "@vueuse/core";
21+
import { defineGlobalCheats, defineGlobalCommands, freezeGlobalConstants } from "@/lib/global";
2222
2323
const i18n = useI18n();
2424
@@ -50,6 +50,7 @@ if (__DESKTOP__) {
5050
5151
onMounted(async () => {
5252
try {
53+
freezeGlobalConstants();
5354
defineGlobalCommands();
5455
defineGlobalCheats();
5556

app/src/components/chat/ChatMessage.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const datetime = computed(() => props.message.formatDate());
1616
<div class="flex w-full flex-col gap-1 overflow-hidden">
1717
<div class="text-muted-foreground flex items-center justify-between gap-4 text-xs">
1818
<span v-if="message.author.kind === 'player'" class="ellipsis">
19-
{{ message.author.id }}
19+
{{ message.author.player }}
2020
</span>
2121
<span class="hidden md:block">{{ datetime }}</span>
2222
</div>

app/src/lib/global.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import * as commands from "@/commands";
55
import * as cheats from "@/commands/cheat";
6+
import { freezeObject } from "@/lib/object";
67
import { camelCase } from "es-toolkit/string";
78

89
export const CONSTS = globalThis.__CONSTS__;
@@ -11,6 +12,10 @@ export const DESKTOP = globalThis.__DESKTOP__;
1112
export const MOBILE = globalThis.__MOBILE__;
1213
export const VERSION = globalThis.__VERSION__;
1314

15+
export function freezeGlobalConstants() {
16+
freezeObject(globalThis.__CONSTS__);
17+
}
18+
1419
export function defineGlobalCommands() {
1520
if (!Object.hasOwn(globalThis.NIL, "cmd")) {
1621
Object.defineProperty(globalThis.NIL, "cmd", {

app/src/lib/object.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (C) Call of Nil contributors
2+
// SPDX-License-Identifier: AGPL-3.0-only
3+
4+
import { isPlainObject } from "es-toolkit/predicate";
5+
6+
export function freezeObject(object: Record<PropertyKey, unknown>) {
7+
Object.freeze(object);
8+
9+
for (const key of Reflect.ownKeys(object)) {
10+
const value = object[key];
11+
if (isPlainObject(value)) {
12+
freezeObject(value);
13+
}
14+
}
15+
}

app/src/scenes/game/FooterChat.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function onChatMessage({ message }: ChatMessagePayload) {
4141
!isChatOpen.value &&
4242
route.name !== ("chat" satisfies GameScene) &&
4343
message.author.kind === "player" &&
44-
message.author.id !== player.value?.id
44+
message.author.player !== player.value?.id
4545
) {
4646
hasUnread.value = true;
4747
}

crates/nil-core/src/chat.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use jiff::Zoned;
88
use serde::{Deserialize, Serialize};
99
use std::borrow::Cow;
1010
use std::collections::VecDeque;
11-
use std::num::NonZeroUsize;
11+
use std::num::NonZeroU16;
1212
use std::sync::Arc;
1313
use strum::EnumIs;
1414
use uuid::Uuid;
@@ -38,16 +38,16 @@ impl Chat {
3838
pub struct ChatHistory {
3939
#[cfg_attr(feature = "typescript", ts(as = "Vec<ChatMessage>"))]
4040
queue: VecDeque<ChatMessage>,
41-
size: NonZeroUsize,
41+
size: NonZeroU16,
4242
}
4343

4444
impl ChatHistory {
45-
pub const MIN: NonZeroUsize = NonZeroUsize::new(100).unwrap();
46-
pub const MAX: NonZeroUsize = NonZeroUsize::new(500).unwrap();
45+
pub const MIN: NonZeroU16 = NonZeroU16::new(100).unwrap();
46+
pub const MAX: NonZeroU16 = NonZeroU16::new(500).unwrap();
4747

48-
const fn new(size: usize) -> Self {
48+
const fn new(size: u16) -> Self {
4949
let size = size.clamp(Self::MIN.get(), Self::MAX.get());
50-
let size = unsafe { NonZeroUsize::new_unchecked(size) };
50+
let size = unsafe { NonZeroU16::new_unchecked(size) };
5151
Self { queue: VecDeque::new(), size }
5252
}
5353

@@ -57,8 +57,8 @@ impl ChatHistory {
5757
}
5858

5959
fn prune(&mut self) {
60-
let size = self.size.get();
6160
let len = self.queue.len();
61+
let size = usize::from(self.size.get());
6262
if len >= size {
6363
self.queue.drain(..=(len - size));
6464
}
@@ -144,19 +144,19 @@ pub enum ChatMessageAuthor {
144144
#[default]
145145
System,
146146
Player {
147-
id: PlayerId,
147+
player: PlayerId,
148148
},
149149
}
150150

151151
impl From<PlayerId> for ChatMessageAuthor {
152152
fn from(id: PlayerId) -> Self {
153-
Self::Player { id }
153+
Self::Player { player: id }
154154
}
155155
}
156156

157157
impl From<&PlayerId> for ChatMessageAuthor {
158158
fn from(id: &PlayerId) -> Self {
159-
Self::Player { id: id.clone() }
159+
Self::Player { player: id.clone() }
160160
}
161161
}
162162

crates/nil-ffi/src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ use tokio::sync::RwLock;
1111
pub(crate) static CLIENT: LazyLock<RwLock<Client>> = LazyLock::new(RwLock::default);
1212

1313
pub(crate) fn on_event() -> impl Fn(Event) -> BoxFuture<'static, ()> {
14-
move |event: Event| Box::pin(async move { push_event(event) })
14+
move |event: Event| Box::pin(async move { push_event(&event) })
1515
}

0 commit comments

Comments
 (0)