Skip to content

Commit

Permalink
Move skillName logic to wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Oct 1, 2023
1 parent 055d4ed commit 6bb0c85
Show file tree
Hide file tree
Showing 5 changed files with 462 additions and 119 deletions.
17 changes: 1 addition & 16 deletions src/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ import enums from './enum.json' assert { type: 'json' };
import { randint } from './util.js';
import * as wasm from './rs/pkg/etg.js';

function decodeSkillName(cell) {
const skid = cell & 0xffff,
n = enums.Skill[skid],
c = enums.SkillParams[skid] ?? 0;
return c === 0
? n
: c === 1
? `${n} ${cell >> 16}`
: `${n} ${(((cell >> 16) & 0xff) << 24) >> 24} ${cell >> 24}`;
}

export default class Game {
constructor(data) {
this.game = new wasm.Game(
Expand Down Expand Up @@ -138,11 +127,7 @@ export default class Game {
);
}
getSkill(id, k) {
const name = Array.from(
this.get_one_skill(id, enums.EventId[k]),
decodeSkillName,
);
if (name.length) return name;
return this.get_one_skill(id, enums.EventId[k]);
}
tgtToPos(id, p1id) {
const pos = this.tgt_to_pos(id, p1id);
Expand Down
87 changes: 21 additions & 66 deletions src/rs/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@ use serde_json::Value;

#[derive(Default, Serialize)]
struct Enums {
Event: BTreeMap<u16, String>,
EventId: BTreeMap<String, u16>,
Flag: BTreeMap<u16, String>,
FlagId: BTreeMap<String, u16>,
Fx: BTreeMap<u16, String>,
Skill: BTreeMap<u16, String>,
SkillParams: BTreeMap<u16, u16>,
Stat: BTreeMap<u16, String>,
StatId: BTreeMap<String, u16>,
}

Expand Down Expand Up @@ -182,35 +177,30 @@ fn process_cards(set: &'static str, path: &'static str, source: &mut String, enu
if event.starts_with(b"Own") {
event[3] -= b'a' - b'A';
}
if skill == b"static" {
skill.insert(0, b'r');
skill.insert(1, b'#');
} else {
let mut idx = 0;
loop {
let mut replaced = false;
while idx < skill.len() {
let ch = skill[idx];
if ch == b' ' {
skill[idx] = if replaced { b',' } else { b'(' };
replaced = true;
} else if ch == b',' {
idx += 1;
break;
}
idx += 1;
}
if replaced {
skill.insert(idx - (idx < skill.len()) as usize, b')');
let mut idx = 0;
loop {
let mut replaced = false;
while idx < skill.len() {
let ch = skill[idx];
if ch == b' ' {
skill[idx] = if replaced { b',' } else { b'(' };
replaced = true;
} else if ch == b',' {
idx += 1;
}
if idx < skill.len() {
skill.splice(idx..idx, b"Skill::".iter().cloned());
idx += "Skill::".len();
continue;
} else {
break;
}
idx += 1;
}
if replaced {
skill.insert(idx - (idx < skill.len()) as usize, b')');
idx += 1;
}
if idx < skill.len() {
skill.splice(idx..idx, b"Skill::".iter().cloned());
idx += "Skill::".len();
continue;
} else {
break;
}
}
write!(
Expand Down Expand Up @@ -298,45 +288,12 @@ fn main() {
eventid += 1;
let mut ownname = String::from("own");
ownname.push_str(&name);
enums.Event.insert(id, name.clone());
enums.Event.insert(id | 128, ownname.clone());
enums.EventId.insert(name, id);
enums.EventId.insert(ownname, id | 128);
}
}
}

source.push_str("pub fn id_skill(s:Skill)->i32{match s{\n");
let skillskill = subsource(&skillrs, "pub enum Skill {\n");
let mut skillid = 1;
for line in skillskill.lines() {
let line = line.trim();
if let Some(end) = line.rfind(",") {
let start = if line.starts_with("r#") { 2 } else { 0 };
let line = &line[start..end];
let end = end - start;
let id = skillid;
skillid += 1;
let skillend = line.find("(");
let name = &line[..skillend.unwrap_or(end)];
enums.Skill.insert(id, String::from(name));
source.push_str("Skill::");
if name == "static" {
source.push_str("r#");
}
source.push_str(name);
if let Some(skillend) = skillend {
enums.SkillParams.insert(
id,
line[skillend..].bytes().filter(|&c| c == b',').count() as u16 + 1,
);
source.push_str("(..)")
}
write!(source, "=>{},\n", id).ok();
}
}
source.push_str("}}\n");

source.push_str("pub fn id_stat(s:Stat)->i32{match s{\n");
let gamestat = subsource(&gamers, "pub enum Stat {\n");
let mut stat_source = String::from("pub fn stat_id(s:i32)->Option<Stat>{Some(match s{\n");
Expand All @@ -347,7 +304,6 @@ fn main() {
let name = line[start..end].trim();
let id = statid;
statid += 1;
enums.Stat.insert(id, String::from(name));
enums.StatId.insert(String::from(name), id);
write!(source, "Stat::{}=>{},\n", name, id).ok();
write!(stat_source, "{}=>Stat::{},\n", id, name).ok();
Expand All @@ -367,7 +323,6 @@ fn main() {
let name = &line["pub const ".len()..colonidx];
let id = statid;
statid += 1;
enums.Flag.insert(id, String::from(name));
enums.FlagId.insert(String::from(name), id);
write!(source, "Flag::{}=>{},\n", name, id).ok();
write!(flag_source, "{}=>Flag::{},\n", id, name).ok();
Expand Down
28 changes: 18 additions & 10 deletions src/rs/src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use alloc::borrow::Cow;
use alloc::rc::Rc;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use core::cmp;
Expand Down Expand Up @@ -883,18 +884,25 @@ impl Game {
}
}

pub fn get_one_skill(&self, id: i32, k: u8) -> Vec<i32> {
pub fn get_one_skill(&self, id: i32, k: u8) -> Option<String> {
if let Ok(event) = Event::try_from(k) {
self.get_thing(id)
.skill
.get(event)
.map(|sk| sk.as_ref())
.unwrap_or(&[])
.iter()
.map(|&sk| generated::id_skill(sk) | sk.param1() << 16 | sk.param2() << 24)
.collect()
if let Some(sk) = self.get_thing(id).skill.get(event) {
if sk.is_empty() {
None
} else {
let mut name = String::new();
for s in sk.iter() {
s.push_name(self, id, &mut name);
name.push(' ');
}
name.truncate(name.len() - 1);
Some(name)
}
} else {
None
}
} else {
Vec::new()
None
}
}

Expand Down
Loading

0 comments on commit 6bb0c85

Please sign in to comment.