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
use alloc::boxed::Box;
use alloc::string::{ToString, String};

#[allow(unused_imports)]
use self::super::root;
#[repr(C)]
#[derive(Debug)]
pub struct PlayReport {
    pub event_id: [u8;32],
    pub buffer: *const u8,
    pub size: usize,
    pub position: usize
}

#[repr(C)]
pub struct Any64BitId {
    pub id: i64
}

const EventIdLengthMax: usize = 31;
const KeyLengthMax: usize = 63;

extern "C" {
    #[link_name = "\u{1}_ZN2nn5prepo10PlayReport10SetEventIdEPKc"]
    pub fn PlayReport_SetEventId(
        this: *mut PlayReport,
        event_id: *const u8,
    ) -> root::Result;

    #[link_name = "\u{1}_ZN2nn5prepo10PlayReport9SetBufferEPvm"]
    pub fn PlayReport_SetBuffer(this: *mut PlayReport, buf: *const u8, size: usize);

    #[link_name = "\u{1}_ZN2nn5prepo10PlayReport3AddEPKcl"]
    pub fn PlayReport_AddLong(
        this: *mut PlayReport,
        key: *const u8,
        value: i64,
    ) -> root::Result;

    #[link_name = "\u{1}_ZN2nn5prepo10PlayReport3AddEPKcd"]
    pub fn PlayReport_AddDouble(
        this: *mut PlayReport,
        key: *const u8,
        value: f64,
    ) -> root::Result;

    #[link_name = "\u{1}_ZN2nn5prepo10PlayReport3AddEPKcS3_"]
    pub fn PlayReport_AddString(
        this: *mut PlayReport,
        key: *const u8,
        value: *const u8,
    ) -> root::Result;

    #[link_name = "\u{1}_ZN2nn5prepo10PlayReport3AddEPKcRKNS0_10Any64BitIdE"]
    pub fn PlayReport_AddAny64BitID(
            this: *mut PlayReport,
            key: *const u8,
            value: Any64BitId,
    ) -> root::Result;

    #[link_name = "\u{1}_ZN2nn5prepo10PlayReport4SaveEv"]
    pub fn PlayReport_Save(this: PlayReport) -> root::Result;

    #[link_name = "\u{1}_ZN2nn5prepo10PlayReport4SaveERKNS_7account3UidE"]
    pub fn PlayReport_SaveWithUserId(
        this: PlayReport,
        uid: *const root::nn::account::Uid,
    ) -> root::Result;

    #[link_name = "\u{1}_ZN2nn5prepo10PlayReportC1Ev"]
    pub fn PlayReport_PlayReport(this: *mut PlayReport);

    #[link_name = "_ZN2nn5prepo10PlayReportC1EPKc"]
    pub fn PlayReport_PlayReportWithEventID(this: *mut PlayReport, event_id: *const u8);

    #[link_name = "\u{1}_ZNK2nn5prepo10PlayReport8GetCountEv"]
    pub fn PlayReport_GetCount(this: *mut PlayReport) -> u64;

    #[link_name = "_ZN2nn5prepo28RequestImmediateTransmissionEv"]
    pub fn RequestImmediateTransmission() -> root::Result;
}
impl PlayReport {
    #[inline]
    pub fn get_count(&mut self) -> u64 {
        unsafe { PlayReport_GetCount(self) }
    }
    #[inline]
    pub fn set_event_id(&mut self, event_id: &str) -> root::Result {
        let event_id = event_id.to_string() + "\0";
        if event_id.len() > EventIdLengthMax {
            panic!("Event ID is too long!");
        }
        let event_id = event_id.as_bytes().as_ptr();
        unsafe { PlayReport_SetEventId(self, event_id) }
    }
    #[inline]
    fn set_buffer(&mut self, buf: *const u8, size: usize) {
        unsafe { PlayReport_SetBuffer(self, buf, size) }
    }
    #[inline]
    pub fn add_long(
        &mut self,
        key: &str,
        value: i64,
    ) -> root::Result {
        let key = key.to_string() + "\0";
        if key.len() > KeyLengthMax {
            panic!("Key is too long!");
        }
        let key = key.as_bytes().as_ptr();
        unsafe { PlayReport_AddLong(self, key, value) }
    }
    #[inline]
    pub fn add_double(
        &mut self,
        key: &str,
        value: f64,
    ) -> root::Result {
        let key = key.to_string() + "\0";
        if key.len() > KeyLengthMax {
            panic!("Key is too long!");
        }
        let key = key.as_bytes().as_ptr();
        unsafe { PlayReport_AddDouble(self, key, value) }
    }
    #[inline]
    pub fn add_string(
        &mut self,
        key: &str,
        value: &str,
    ) -> root::Result {
        let key = key.to_string() + "\0";
        if key.len() > KeyLengthMax {
            panic!("Key is too long!");
        }
        let key = key.as_bytes().as_ptr();

        let value = value.to_string() + "\0";
        let value = value.as_bytes().as_ptr();
        unsafe { PlayReport_AddString(self, key, value) }
    }
    #[inline]
    pub fn add_any64bitid(
        &mut self,
        key: &str,
        value: Any64BitId,
    ) -> root::Result {
        let key = key.to_string() + "\0";
        if key.len() > KeyLengthMax {
            panic!("Key is too long!");
        }
        let key = key.as_bytes().as_ptr();
        unsafe { PlayReport_AddAny64BitID(self, key, value) }
    }
    #[inline]
    pub fn save(self) -> root::Result {
        unsafe { PlayReport_Save(self) }
    }
    #[inline]
    pub fn save_with_user_id(
        self,
        uid: *const root::nn::account::Uid,
    ) -> root::Result {
        unsafe { PlayReport_SaveWithUserId(self, uid) }
    }
    #[inline]
    pub fn save_for_current_user(self) {
        // This provides a UserHandle and sets the User in a Open state to be used.
        let handle = root::nn::account::try_open_preselected_user().expect("OpenPreselectedUser should not return false");
        // Obtain the UID for this user
        let uid = root::nn::account::get_user_id(&handle).expect("GetUserId should return a valid Uid");
        self.save_with_user_id(&uid);
        root::nn::account::close_user(handle);
    }
    #[inline]
    pub fn new() -> Self {
        let buf = Box::new([0u8; 0x4000]);
        let buf = Box::leak(buf);
        let buf = Box::new([0u8; 0x4000]);
        let buf = Box::leak(buf);
        let mut prepo: PlayReport = PlayReport { event_id: [0;32], buffer: core::ptr::null(), size: 0, position: 0 };
        unsafe { PlayReport_PlayReport(&mut prepo) };

        prepo.set_buffer(buf.as_ptr(), 0x4000);
        prepo
    }
    #[inline]
    pub fn new_with_event_id(event_id: &str) -> Self {
        let event_id = event_id.to_string() + "\0";
        if event_id.len() > EventIdLengthMax {
            panic!("Event ID is too long!");
        }
        let event_id = event_id.as_bytes().as_ptr();

        let buf = Box::new([0u8; 0x4000]);
        let buf = Box::leak(buf);
        let mut prepo: PlayReport = PlayReport { event_id: [0;32], buffer: core::ptr::null(), size: 0, position: 0 };
        unsafe { PlayReport_PlayReportWithEventID(&mut prepo, event_id) };

        prepo.set_buffer(buf.as_ptr(), 0x4000);
        prepo
    }
}

impl Drop for PlayReport {
    fn drop(&mut self) {
        unsafe { libc::free(self.buffer as *mut libc::c_void); }
    }
}