mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 years ago
9 changed files with 367 additions and 109 deletions
@ -1,5 +1,24 @@ |
|||||
extern crate klotski_ffi; |
extern crate klotski_ffi; |
||||
|
|
||||
fn main() { |
fn main() { |
||||
println!("{:#?}", klotski_ffi::load_metadata()); |
// println!("{:#?}", klotski_ffi::load_metadata());
|
||||
|
|
||||
|
// println!("{}", klotski_ffi::is_short_code_available());
|
||||
|
// println!("{}", klotski_ffi::is_short_code_available_fast());
|
||||
|
//
|
||||
|
// klotski_ffi::short_code_enable();
|
||||
|
//
|
||||
|
// println!("{}", klotski_ffi::is_short_code_available());
|
||||
|
// println!("{}", klotski_ffi::is_short_code_available_fast());
|
||||
|
//
|
||||
|
// klotski_ffi::short_code_enable_fast();
|
||||
|
//
|
||||
|
// println!("{}", klotski_ffi::is_short_code_available());
|
||||
|
// println!("{}", klotski_ffi::is_short_code_available_fast());
|
||||
|
|
||||
|
// let raw_code = klotski_ffi::common_code_to_raw_code(0x1A9BF0C00).unwrap();
|
||||
|
// println!("raw code = {}", raw_code);
|
||||
|
|
||||
|
println!("short code: {}", klotski_ffi::short_code_to_string(12345).unwrap()); |
||||
|
|
||||
} |
} |
||||
|
@ -1,50 +1,50 @@ |
|||||
#include "klotski.h" |
#include "klotski.h" |
||||
#include "metadata.h" |
#include "metadata.h" |
||||
|
|
||||
uint32_t get_version_major() noexcept { |
uint32_t get_version_major() { |
||||
return VERSION_MAJOR; |
return VERSION_MAJOR; |
||||
} |
} |
||||
|
|
||||
uint32_t get_version_minor() noexcept { |
uint32_t get_version_minor() { |
||||
return VERSION_MINOR; |
return VERSION_MINOR; |
||||
} |
} |
||||
|
|
||||
uint32_t get_version_patch() noexcept { |
uint32_t get_version_patch() { |
||||
return VERSION_PATCH; |
return VERSION_PATCH; |
||||
} |
} |
||||
|
|
||||
const char* get_author() noexcept { |
const char* get_author() { |
||||
return AUTHOR; |
return AUTHOR; |
||||
} |
} |
||||
|
|
||||
const char* get_git_tag() noexcept { |
const char* get_git_tag() { |
||||
return GIT_TAG_ID; |
return GIT_TAG_ID; |
||||
} |
} |
||||
|
|
||||
const char* get_version() noexcept { |
const char* get_version() { |
||||
return VERSION_STR; |
return VERSION_STR; |
||||
} |
} |
||||
|
|
||||
const char* get_commit_id() noexcept { |
const char* get_commit_id() { |
||||
return GIT_COMMIT_ID; |
return GIT_COMMIT_ID; |
||||
} |
} |
||||
|
|
||||
const char* get_build_time() noexcept { |
const char* get_build_time() { |
||||
return BUILD_TIME; |
return BUILD_TIME; |
||||
} |
} |
||||
|
|
||||
const char* get_git_branch() noexcept { |
const char* get_git_branch() { |
||||
return GIT_BRANCH; |
return GIT_BRANCH; |
||||
} |
} |
||||
|
|
||||
const char* get_project_url() noexcept { |
const char* get_project_url() { |
||||
return GIT_PROJECT; |
return GIT_PROJECT; |
||||
} |
} |
||||
|
|
||||
const char* get_system_info() noexcept { |
const char* get_system_info() { |
||||
return SYSTEM; |
return SYSTEM; |
||||
} |
} |
||||
|
|
||||
const char* get_compiler_info() noexcept { |
const char* get_compiler_info() { |
||||
return COMPILER; |
return COMPILER; |
||||
} |
} |
||||
|
@ -0,0 +1,234 @@ |
|||||
|
use std::f32::consts::E; |
||||
|
use std::ffi::{c_char, CString}; |
||||
|
use super::Core; |
||||
|
|
||||
|
fn short_code_enable() { |
||||
|
unsafe { |
||||
|
Core::short_code_enable() |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn short_code_enable_fast() { |
||||
|
unsafe { |
||||
|
Core::short_code_enable_fast() |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn is_short_code_available() -> bool { |
||||
|
unsafe { |
||||
|
Core::is_short_code_available() |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn is_short_code_available_fast() -> bool { |
||||
|
unsafe { |
||||
|
Core::is_short_code_available_fast() |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn raw_code_check(raw_code: u64) -> bool { |
||||
|
unsafe { |
||||
|
Core::raw_code_check(raw_code) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn short_code_check(short_code: u32) -> bool { |
||||
|
unsafe { |
||||
|
Core::short_code_check(short_code) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn common_code_check(common_code: u64) -> bool { |
||||
|
unsafe { |
||||
|
Core::common_code_check(common_code) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn raw_code_to_short_code(raw_code: u64) -> Result<u32, &'static str> { |
||||
|
let mut short_code: u32 = 0; |
||||
|
unsafe { |
||||
|
match Core::raw_code_to_short_code(raw_code, &mut short_code) { |
||||
|
true => Ok(short_code), |
||||
|
_ => Err("invalid raw code"), |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn short_code_to_raw_code(short_code: u32) -> Result<u64, &'static str> { |
||||
|
let mut raw_code: u64 = 0; |
||||
|
unsafe { |
||||
|
match Core::short_code_to_raw_code(short_code, &mut raw_code) { |
||||
|
true => Ok(raw_code), |
||||
|
_ => Err("invalid short code"), |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn raw_code_to_common_code(raw_code: u64) -> Result<u64, &'static str> { |
||||
|
let mut common_code: u64 = 0; |
||||
|
unsafe { |
||||
|
match Core::raw_code_to_common_code(raw_code, &mut common_code) { |
||||
|
true => Ok(common_code), |
||||
|
_ => Err("invalid raw code"), |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn common_code_to_raw_code(common_code: u64) -> Result<u64, &'static str> { |
||||
|
let mut raw_code: u64 = 0; |
||||
|
unsafe { |
||||
|
match Core::common_code_to_raw_code(common_code, &mut raw_code) { |
||||
|
true => Ok(raw_code), |
||||
|
_ => Err("invalid common code"), |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn short_code_to_common_code(short_code: u32) -> Result<u64, &'static str> { |
||||
|
let mut common_code: u64 = 0; |
||||
|
unsafe { |
||||
|
match Core::short_code_to_common_code(short_code, &mut common_code) { |
||||
|
true => Ok(common_code), |
||||
|
_ => Err("invalid short code"), |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn common_code_to_short_code(common_code: u64) -> Result<u32, &'static str> { |
||||
|
let mut short_code: u32 = 0; |
||||
|
unsafe { |
||||
|
match Core::common_code_to_short_code(common_code, &mut short_code) { |
||||
|
true => Ok(short_code), |
||||
|
_ => Err("invalid common code"), |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn raw_code_to_short_code_unsafe(raw_code: u64) -> u32 { |
||||
|
unsafe { |
||||
|
Core::raw_code_to_short_code_unsafe(raw_code) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn short_code_to_raw_code_unsafe(short_code: u32) -> u64 { |
||||
|
unsafe { |
||||
|
Core::short_code_to_raw_code_unsafe(short_code) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn raw_code_to_common_code_unsafe(raw_code: u64) -> u64 { |
||||
|
unsafe { |
||||
|
Core::raw_code_to_common_code_unsafe(raw_code) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn common_code_to_raw_code_unsafe(common_code: u64) -> u64 { |
||||
|
unsafe { |
||||
|
Core::common_code_to_raw_code_unsafe(common_code) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn short_code_to_common_code_unsafe(short_code: u32) -> u64 { |
||||
|
unsafe { |
||||
|
Core::short_code_to_common_code_unsafe(short_code) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn common_code_to_short_code_unsafe(common_code: u64) -> u32 { |
||||
|
unsafe { |
||||
|
Core::common_code_to_short_code_unsafe(common_code) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn is_vertical_mirror(raw_code: u64) -> Result<bool, &'static str> { |
||||
|
let mut result: bool = false; |
||||
|
unsafe { |
||||
|
match Core::is_vertical_mirror(raw_code, &mut result) { |
||||
|
true => Ok(result), |
||||
|
_ => Err("invalid raw code"), |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn is_horizontal_mirror(raw_code: u64) -> Result<bool, &'static str> { |
||||
|
let mut result: bool = false; |
||||
|
unsafe { |
||||
|
match Core::is_horizontal_mirror(raw_code, &mut result) { |
||||
|
true => Ok(result), |
||||
|
_ => Err("invalid raw code"), |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn to_vertical_mirror(raw_code: u64) -> Result<u64, &'static str> { |
||||
|
let mut result: u64 = 0; |
||||
|
unsafe { |
||||
|
match Core::to_vertical_mirror(raw_code, &mut result) { |
||||
|
true => Ok(result), |
||||
|
_ => Err("invalid raw code"), |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn to_horizontal_mirror(raw_code: u64) -> Result<u64, &'static str> { |
||||
|
let mut result: u64 = 0; |
||||
|
unsafe { |
||||
|
match Core::to_horizontal_mirror(raw_code, &mut result) { |
||||
|
true => Ok(result), |
||||
|
_ => Err("invalid raw code"), |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn is_vertical_mirror_unsafe(raw_code: u64) -> bool { |
||||
|
unsafe { |
||||
|
Core::is_vertical_mirror_unsafe(raw_code) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn is_horizontal_mirror_unsafe(raw_code: u64) -> bool { |
||||
|
unsafe { |
||||
|
Core::is_horizontal_mirror_unsafe(raw_code) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn to_vertical_mirror_unsafe(raw_code: u64) -> u64 { |
||||
|
unsafe { |
||||
|
Core::to_vertical_mirror_unsafe(raw_code) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
fn to_horizontal_mirror_unsafe(raw_code: u64) -> u64 { |
||||
|
unsafe { |
||||
|
Core::to_horizontal_mirror_unsafe(raw_code) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// extern const uint32_t SHORT_CODE_STR_SIZE;
|
||||
|
// EXTERN_FUNC bool short_code_to_string(uint32_t short_code, char short_code_str[]) NOEXCEPT;
|
||||
|
// EXTERN_FUNC bool short_code_from_string(const char short_code_str[], uint32_t *short_code) NOEXCEPT;
|
||||
|
|
||||
|
pub fn short_code_to_string(short_code: u32) -> Result<String, &'static str> { |
||||
|
unsafe { |
||||
|
let mut buffer: Vec<c_char> = vec![0; Core::SHORT_CODE_STR_SIZE as usize]; |
||||
|
match Core::short_code_to_string(short_code, buffer.as_mut_ptr()) { |
||||
|
true => { |
||||
|
let mut result = String::new(); |
||||
|
for c in &buffer[..buffer.len() - 1] { |
||||
|
result.push(*c as u8 as char); |
||||
|
} |
||||
|
Ok(result) |
||||
|
}, |
||||
|
_ => Err("invalid short code"), |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// TODO: add unsafe version
|
||||
|
|
||||
|
// extern const uint32_t COMMON_CODE_STR_SIZE;
|
||||
|
// EXTERN_FUNC bool common_code_to_string(uint64_t common_code, char common_code_str[]) NOEXCEPT;
|
||||
|
// EXTERN_FUNC bool common_code_to_string_shorten(uint64_t common_code, char common_code_str[]) NOEXCEPT;
|
||||
|
// EXTERN_FUNC bool common_code_from_string(const char common_code_str[], uint64_t *common_code) NOEXCEPT;
|
||||
|
|
||||
|
// TODO: add unsafe version
|
@ -0,0 +1,3 @@ |
|||||
|
pub mod ffi; |
||||
|
|
||||
|
use super::core::Core; |
@ -1,4 +1,7 @@ |
|||||
mod core; |
mod core; |
||||
|
mod codec; |
||||
mod metadata; |
mod metadata; |
||||
|
|
||||
pub use metadata::load_metadata; |
pub use metadata::load_metadata; |
||||
|
|
||||
|
pub use codec::ffi::*; |
||||
|
Loading…
Reference in new issue