Skip to content
This repository has been archived by the owner on Sep 22, 2019. It is now read-only.

Commit

Permalink
Fix sampsdk crate source. Fix not copying to dest correctly. Add asse…
Browse files Browse the repository at this point in the history
…rt. Move stuff to include.
  • Loading branch information
Nikola Yanakiev committed Jan 7, 2019
1 parent 5d97129 commit 42f7474
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ name = "pawn_templates"
crate-type = ["dylib"]

[dependencies]
samp-sdk = { git="https://github.com/Southclaws/samp-sdk", branch="remove-detour-dep" }
samp-sdk = { git="https://github.com/ZOTTCE/samp-sdk" }
liquid = "0.18"

[features]
Expand Down
18 changes: 13 additions & 5 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ impl Templates {

let mut parser = args::Parser::new(params);
expand_args!(@amx, parser, template_id: Cell);
expand_args!(@amx, parser, output_string: &mut Cell);
expand_args!(@amx, parser, output_string_amx: Cell);
let output_string = amx.get_address(output_string_amx)?;
expand_args!(@amx, parser, output_length: usize);

log!(
Expand Down Expand Up @@ -153,8 +154,8 @@ impl Templates {

log!("Rendered output: {}", output);

let bytes = String::into_bytes(output);
set_string!(bytes, output_string, output_length);
let encoded = samp_sdk::cp1251::encode(&output)?;
set_string!(encoded, output_string, output_length);

return Ok(0);
}
Expand Down Expand Up @@ -182,6 +183,13 @@ fn get_arg_ref<T: Clone>(amx: &AMX, parser: &mut args::Parser, out_ref: &mut T)

fn get_arg_string(amx: &AMX, parser: &mut args::Parser, out_str: &mut String) -> i32 {
expand_args!(@amx, parser, tmp_str: String);
*out_str = tmp_str;
return 1;
match samp_sdk::cp1251::decode_to(&tmp_str.into_bytes(), out_str) {
Ok(_) => {
return 1;
},
Err(e) => {
log!("{}", e);
return 0;
}
}
}
9 changes: 9 additions & 0 deletions templates.inc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
#endif
#define _templates_included

enum {
TYPE_STR = 1,
TYPE_INT,
TYPE_FLOAT
}

#define PAIR_STR(%0,%1) TYPE_STR, %0, %1
#define PAIR_INT(%0,%1) TYPE_INT, %0, %1
#define PAIR_FLOAT(%0,%1) TYPE_FLOAT, %0, _:%1

native Template:CreateTemplate(const template[]);
native RenderTemplate(Template:id, dest[], len, ...);
20 changes: 4 additions & 16 deletions test.pwn
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,17 @@ main() {
//
}

enum {
TYPE_STR = 1,
TYPE_INT,
TYPE_FLOAT
}

#define PAIR_STR(%0,%1) TYPE_STR, %0, %1
#define PAIR_INT(%0,%1) (TYPE_INT, %0, %1)
#define PAIR_FLOAT(%0,%1) (TYPE_FLOAT, %0, %1)
#define PAIR_REF(%0,%1) (TYPE_REF, %0, %1)


Test:Simple() {
new Template:t = CreateTemplate("Hello, {{ name }}! Today is {{ date }}");
new rendered[64];
new ret = RenderTemplate(t, rendered, sizeof rendered,
TYPE_STR, "name", "Southclaws",
TYPE_STR, "date", "Monday"
PAIR_STR("name", "Southclaws"),
PAIR_STR("date", "Monday")
);

printf("ret: %d", ret);
printf("rendered: %s", rendered);

ASSERT(ret == 0);
// ASSERT(!strcmp(rendered, "Hello, Southclaws!"));
new bool:res = !strcmp(rendered, "Hello, Southclaws! Today is Monday");
ASSERT(res);
}

0 comments on commit 42f7474

Please sign in to comment.