1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/// Operating system module (Windows, Application, Input).
pub mod os;

/// Graphics and compute abstraction module.
pub mod gfx;

/// Hardware accelerated audio and video decoding.
pub mod av;

/// Image reading/writing module support for (png, jpg, bmp, tiff, dds).
pub mod image;

/// Imgui rendering and platform implementation.
pub mod imgui;

/// Immediate mode primitive rendering API.
pub mod imdraw;

/// High level graphics (data driven render pipelines, shaders, views).
pub mod pmfx;

/// Primitive geometry meshes (quad, cube, sphere, etc).
pub mod primitives;

/// Hotline clinet context contains an `App`, `Device`, `SwapChain` and main `Window` automatically setup
/// It can load code dynamically from other `dylibs` or `dlls` abnd provides a very thin run loop for you to hook your own plugins into.
pub mod client;

/// Trait's and macros to assist the creation of plugins in other dynamically loaded libraries
pub mod plugin;

/// Module to aid data / code file watching, rebuilding and reloading
pub mod reloader;

/// Shared types and resources for use with bevy ecs
pub mod ecs_base;

/// Use bitmask for flags
#[macro_use]
extern crate bitflags;

/// Generic errors for modules to define their own
pub struct Error {
    pub msg: String,
}

/// Generic debug for errors
impl std::fmt::Debug for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.msg)
    }
}

/// Conversion for windows-rs win32 errors
#[cfg(target_os = "windows")]
impl From<windows::core::Error> for Error {
    fn from(err: windows::core::Error) -> Error {
        Error {
            msg: err.message().to_string_lossy(),
        }
    }
}

/// std errors
impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Error {
        Error {
            msg: err.to_string()
        }
    }
}

/// Returns the config name for the current configuration, this is useful to local items in target/debug
#[cfg(debug_assertions)]
pub const fn get_config_name() -> &'static str {
    "debug"
}

/// Returns the config name for the current configuration, this is useful to local items in target/release
#[cfg(not(debug_assertions))]
pub const fn get_config_name() -> &'static str {
    "release"
}

/// Return an absolute path for a resource given the relative resource name from the /hotline-data/src_data dir
pub fn get_src_data_path(asset: &str) -> String {
    let exe_path = std::env::current_exe().ok().unwrap();
    let asset_path = exe_path.parent().unwrap().join("../../../hotline-data/src");
    let path = std::fs::canonicalize(asset_path.join(asset)).unwrap();
    String::from(path.to_str().unwrap()).replace("\\\\?\\", "")
}

/// Return an absolute path for a resource given the relative resource name from the /data dir
pub fn get_data_path(asset: &str) -> String {
    let exe_path = std::env::current_exe().ok().unwrap();
    let asset_path = exe_path.parent().unwrap().join("..");
    if asset_path.join("data").exists() {
        let path = std::fs::canonicalize(asset_path.join(asset)).unwrap();
        String::from(path.to_str().unwrap()).replace("\\\\?\\", "")
    }
    else {
        let asset_path = asset_path.join("..");
        if asset_path.join("data").exists() {
            let path = std::fs::canonicalize(asset_path.join(asset)).unwrap();
            String::from(path.to_str().unwrap()).replace("\\\\?\\", "")
        }
        else {
            // unable to locate data
            panic!()
        }
    }
}

/// Return an absolute path for a resource given the relative path from the /executable dir
pub fn get_exe_path(asset: &str) -> String {
    let exe_path = std::env::current_exe().ok().unwrap();
    println!("{}", String::from(exe_path.join(asset).to_str().unwrap()));
    String::from(exe_path.join(asset).to_str().unwrap())
}

/// Recursivley get files from folder as a vector
fn get_files_recursive(dir: &str, mut files: Vec<String>) -> Vec<String> {
    let paths = std::fs::read_dir(dir).unwrap();
    for path in paths {
        let path = path.unwrap().path();
        if std::fs::read_dir(&path).is_ok() {
            files = get_files_recursive(path.to_str().unwrap(), files);
        }
        else {
            files.push(path.to_str().unwrap().to_string());
        }   
    }
    files
}

/// This is a hardcoded compile time selection of os backend for windows as win32
#[cfg(target_os = "windows")]
pub use os::win32 as os_platform;

/// This is a hardcoded compile time selection of os backend for windows as d3d12
#[cfg(target_os = "windows")]
pub use gfx::d3d12 as gfx_platform;

/// This is a hardcoded compile time selection of os backend for windows as wmf
#[cfg(target_os = "windows")]
pub use av::wmf as av_platform;

/// Most commonly used re-exported types.
#[cfg(target_os = "windows")]
pub mod prelude {
    #[doc(hidden)]
    pub use crate::{
        // modules
        gfx,
        os,
        client,
        plugin,
        pmfx,
        imgui,

        // platform specific 
        gfx_platform,
        os_platform,
        av_platform,

        // traits
        ecs_base::*,
        gfx::{Device, SwapChain, CmdBuf, Texture, RenderPass},
        os::{App, Window},
        pmfx::Pmfx,
        imgui::ImGui,
        imdraw::ImDraw,
        client::{Client, HotlineInfo, PluginInfo},
        plugin::{Plugin},
        av::{VideoPlayer},

        // macros
        hotline_plugin,
        system_func,
        render_func,
        render_func_closure,
        demos,
        systems
    };
}

#[cfg(not(target_os = "windows"))]
pub mod prelude {
    #[doc(hidden)]
    pub use crate::{
        // modules
        gfx,
        os,
        client,
        plugin,
        pmfx,
        imgui,

        // traits
        gfx::{Device, SwapChain, CmdBuf, Texture, RenderPass},
        os::{App, Window},
        pmfx::Pmfx,
        imgui::ImGui,
        imdraw::ImDraw,
        client::{Client, HotlineInfo, PluginInfo},
        plugin::{Plugin},
        av::{VideoPlayer},
    };
}