Co-authored-by: Nilton Constantino <nilton.constantino@visma.com> Reviewed-on: #8
240 lines
8.2 KiB
Rust
240 lines
8.2 KiB
Rust
use crate::frontends::pbs::types::PbsType;
|
|
use std::collections::HashMap;
|
|
|
|
pub struct ContractMethod {
|
|
pub id: u32,
|
|
pub params: Vec<PbsType>,
|
|
pub return_type: PbsType,
|
|
}
|
|
|
|
pub struct ContractRegistry {
|
|
mappings: HashMap<String, HashMap<String, ContractMethod>>,
|
|
}
|
|
|
|
impl ContractRegistry {
|
|
pub fn new() -> Self {
|
|
let mut mappings = HashMap::new();
|
|
|
|
// GFX mappings
|
|
let mut gfx = HashMap::new();
|
|
gfx.insert("clear".to_string(), ContractMethod {
|
|
id: 0x1010,
|
|
params: vec![PbsType::Struct("Color".to_string())],
|
|
return_type: PbsType::Void,
|
|
});
|
|
gfx.insert("fillRect".to_string(), ContractMethod {
|
|
id: 0x1002,
|
|
params: vec![PbsType::Int, PbsType::Int, PbsType::Int, PbsType::Int, PbsType::Struct("Color".to_string())],
|
|
return_type: PbsType::Void,
|
|
});
|
|
gfx.insert("drawLine".to_string(), ContractMethod {
|
|
id: 0x1003,
|
|
params: vec![PbsType::Int, PbsType::Int, PbsType::Int, PbsType::Int, PbsType::Struct("Color".to_string())],
|
|
return_type: PbsType::Void,
|
|
});
|
|
gfx.insert("drawCircle".to_string(), ContractMethod {
|
|
id: 0x1004,
|
|
params: vec![PbsType::Int, PbsType::Int, PbsType::Int, PbsType::Struct("Color".to_string())],
|
|
return_type: PbsType::Void,
|
|
});
|
|
gfx.insert("drawDisc".to_string(), ContractMethod {
|
|
id: 0x1005,
|
|
params: vec![PbsType::Int, PbsType::Int, PbsType::Int, PbsType::Struct("Color".to_string())],
|
|
return_type: PbsType::Void,
|
|
});
|
|
gfx.insert("drawSquare".to_string(), ContractMethod {
|
|
id: 0x1006,
|
|
params: vec![PbsType::Int, PbsType::Int, PbsType::Int, PbsType::Struct("Color".to_string())],
|
|
return_type: PbsType::Void,
|
|
});
|
|
gfx.insert("setSprite".to_string(), ContractMethod {
|
|
id: 0x1007,
|
|
params: vec![PbsType::Int, PbsType::Int, PbsType::Int],
|
|
return_type: PbsType::Void,
|
|
});
|
|
gfx.insert("drawText".to_string(), ContractMethod {
|
|
id: 0x1008,
|
|
params: vec![PbsType::Int, PbsType::Int, PbsType::String, PbsType::Struct("Color".to_string())],
|
|
return_type: PbsType::Void,
|
|
});
|
|
mappings.insert("Gfx".to_string(), gfx);
|
|
|
|
// Input mappings
|
|
let mut input = HashMap::new();
|
|
input.insert("pad".to_string(), ContractMethod {
|
|
id: 0x2010,
|
|
params: vec![],
|
|
return_type: PbsType::Struct("Pad".to_string()),
|
|
});
|
|
input.insert("touch".to_string(), ContractMethod {
|
|
id: 0x2011,
|
|
params: vec![],
|
|
return_type: PbsType::Struct("Touch".to_string()),
|
|
});
|
|
mappings.insert("Input".to_string(), input);
|
|
|
|
// Touch mappings
|
|
let mut touch = HashMap::new();
|
|
touch.insert("getX".to_string(), ContractMethod {
|
|
id: 0x2101,
|
|
params: vec![],
|
|
return_type: PbsType::Int,
|
|
});
|
|
touch.insert("getY".to_string(), ContractMethod {
|
|
id: 0x2102,
|
|
params: vec![],
|
|
return_type: PbsType::Int,
|
|
});
|
|
touch.insert("isDown".to_string(), ContractMethod {
|
|
id: 0x2103,
|
|
params: vec![],
|
|
return_type: PbsType::Bool,
|
|
});
|
|
touch.insert("isPressed".to_string(), ContractMethod {
|
|
id: 0x2104,
|
|
params: vec![],
|
|
return_type: PbsType::Bool,
|
|
});
|
|
touch.insert("isReleased".to_string(), ContractMethod {
|
|
id: 0x2105,
|
|
params: vec![],
|
|
return_type: PbsType::Bool,
|
|
});
|
|
touch.insert("getHold".to_string(), ContractMethod {
|
|
id: 0x2106,
|
|
params: vec![],
|
|
return_type: PbsType::Int,
|
|
});
|
|
mappings.insert("Touch".to_string(), touch);
|
|
|
|
// Audio mappings
|
|
let mut audio = HashMap::new();
|
|
audio.insert("playSample".to_string(), ContractMethod {
|
|
id: 0x3001,
|
|
params: vec![PbsType::Int],
|
|
return_type: PbsType::Void,
|
|
});
|
|
audio.insert("play".to_string(), ContractMethod {
|
|
id: 0x3002,
|
|
params: vec![PbsType::Int],
|
|
return_type: PbsType::Void,
|
|
});
|
|
mappings.insert("Audio".to_string(), audio);
|
|
|
|
// FS mappings
|
|
let mut fs = HashMap::new();
|
|
fs.insert("open".to_string(), ContractMethod {
|
|
id: 0x4001,
|
|
params: vec![PbsType::String, PbsType::String],
|
|
return_type: PbsType::Int,
|
|
});
|
|
fs.insert("read".to_string(), ContractMethod {
|
|
id: 0x4002,
|
|
params: vec![PbsType::Int],
|
|
return_type: PbsType::String,
|
|
});
|
|
fs.insert("write".to_string(), ContractMethod {
|
|
id: 0x4003,
|
|
params: vec![PbsType::Int, PbsType::String],
|
|
return_type: PbsType::Int,
|
|
});
|
|
fs.insert("close".to_string(), ContractMethod {
|
|
id: 0x4004,
|
|
params: vec![PbsType::Int],
|
|
return_type: PbsType::Void,
|
|
});
|
|
fs.insert("listDir".to_string(), ContractMethod {
|
|
id: 0x4005,
|
|
params: vec![PbsType::String],
|
|
return_type: PbsType::String,
|
|
});
|
|
fs.insert("exists".to_string(), ContractMethod {
|
|
id: 0x4006,
|
|
params: vec![PbsType::String],
|
|
return_type: PbsType::Bool,
|
|
});
|
|
fs.insert("delete".to_string(), ContractMethod {
|
|
id: 0x4007,
|
|
params: vec![PbsType::String],
|
|
return_type: PbsType::Bool,
|
|
});
|
|
mappings.insert("Fs".to_string(), fs);
|
|
|
|
// Log mappings
|
|
let mut log = HashMap::new();
|
|
log.insert("write".to_string(), ContractMethod {
|
|
id: 0x5001,
|
|
params: vec![PbsType::Int, PbsType::String],
|
|
return_type: PbsType::Void,
|
|
});
|
|
log.insert("writeTag".to_string(), ContractMethod {
|
|
id: 0x5002,
|
|
params: vec![PbsType::Int, PbsType::Int, PbsType::String],
|
|
return_type: PbsType::Void,
|
|
});
|
|
mappings.insert("Log".to_string(), log);
|
|
|
|
// System mappings
|
|
let mut system = HashMap::new();
|
|
system.insert("hasCart".to_string(), ContractMethod {
|
|
id: 0x0001,
|
|
params: vec![],
|
|
return_type: PbsType::Bool,
|
|
});
|
|
system.insert("runCart".to_string(), ContractMethod {
|
|
id: 0x0002,
|
|
params: vec![],
|
|
return_type: PbsType::Void,
|
|
});
|
|
mappings.insert("System".to_string(), system);
|
|
|
|
// Asset mappings
|
|
let mut asset = HashMap::new();
|
|
asset.insert("load".to_string(), ContractMethod {
|
|
id: 0x6001,
|
|
params: vec![PbsType::String],
|
|
return_type: PbsType::Int,
|
|
});
|
|
asset.insert("status".to_string(), ContractMethod {
|
|
id: 0x6002,
|
|
params: vec![PbsType::Int],
|
|
return_type: PbsType::Int,
|
|
});
|
|
asset.insert("commit".to_string(), ContractMethod {
|
|
id: 0x6003,
|
|
params: vec![PbsType::Int],
|
|
return_type: PbsType::Void,
|
|
});
|
|
asset.insert("cancel".to_string(), ContractMethod {
|
|
id: 0x6004,
|
|
params: vec![PbsType::Int],
|
|
return_type: PbsType::Void,
|
|
});
|
|
mappings.insert("Asset".to_string(), asset);
|
|
|
|
// Bank mappings
|
|
let mut bank = HashMap::new();
|
|
bank.insert("info".to_string(), ContractMethod {
|
|
id: 0x6101,
|
|
params: vec![PbsType::Int],
|
|
return_type: PbsType::String,
|
|
});
|
|
bank.insert("slotInfo".to_string(), ContractMethod {
|
|
id: 0x6102,
|
|
params: vec![PbsType::Int, PbsType::Int],
|
|
return_type: PbsType::String,
|
|
});
|
|
mappings.insert("Bank".to_string(), bank);
|
|
|
|
Self { mappings }
|
|
}
|
|
|
|
pub fn resolve(&self, contract: &str, method: &str) -> Option<u32> {
|
|
self.mappings.get(contract).and_then(|m| m.get(method)).map(|m| m.id)
|
|
}
|
|
|
|
pub fn get_method(&self, contract: &str, method: &str) -> Option<&ContractMethod> {
|
|
self.mappings.get(contract).and_then(|m| m.get(method))
|
|
}
|
|
}
|