all repos — gemini-redirect @ c06a801426a586a11e94042773d6c1bfc2a457b7

blog/atom.xml (view raw)

   1<?xml version="1.0" encoding="UTF-8"?>
   2<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
   3	<title>Lonami&#x27;s Site - My Blog</title>
   4	<link href="https://lonami.dev/blog/atom.xml" rel="self" type="application/atom+xml"/>
   5  <link href="https://lonami.dev/blog/"/>
   6	<generator uri="https://www.getzola.org/">Zola</generator>
   7	<updated>2021-03-13T00:00:00+00:00</updated>
   8	<id>https://lonami.dev/blog/atom.xml</id>
   9	<entry xml:lang="en">
  10		<title>Writing our own Cheat Engine: Pointers</title>
  11		<published>2021-03-13T00:00:00+00:00</published>
  12		<updated>2021-03-13T00:00:00+00:00</updated>
  13		<link href="https://lonami.dev/blog/woce-6/" type="text/html"/>
  14		<id>https://lonami.dev/blog/woce-6/</id>
  15		<content type="html">&lt;p&gt;This is part 6 on the &lt;em&gt;Writing our own Cheat Engine&lt;&#x2F;em&gt; series:&lt;&#x2F;p&gt;
  16&lt;ul&gt;
  17&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-1&quot;&gt;Part 1: Introduction&lt;&#x2F;a&gt; (start here if you&#x27;re new to the series!)&lt;&#x2F;li&gt;
  18&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-2&quot;&gt;Part 2: Exact Value scanning&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
  19&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-3&quot;&gt;Part 3: Unknown initial value&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
  20&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-4&quot;&gt;Part 4: Floating points&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
  21&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-5&quot;&gt;Part 5: Code finder&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
  22&lt;li&gt;Part 6: Pointers&lt;&#x2F;li&gt;
  23&lt;&#x2F;ul&gt;
  24&lt;p&gt;In part 5 we wrote our very own debugger. We learnt that Cheat Engine is using hardware breakpoints to watch memory change, and how to do the same ourselves. We also learnt that hardware points are not the only way to achieve the effect of watchpoints, although they certainly are the fastest and cleanest approach.&lt;&#x2F;p&gt;
  25&lt;p&gt;In this post, we will be reusing some of that knowledge to find out a closely related value, the &lt;em&gt;pointer&lt;&#x2F;em&gt; that points to the real value&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. As a quick reminder, a pointer is nothing but an &lt;code&gt;usize&lt;&#x2F;code&gt;&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; representing the address of another portion of memory, in this case, the actual value we will be scanning for. A pointer is a value that, well, points elsewhere. In Rust we normally use reference instead, which are safer (typed and their lifetime is tracked) than pointers, but in the end we can achieve the same with both.&lt;&#x2F;p&gt;
  26&lt;p&gt;Why care about pointers? It turns out that things, such as your current health in-game, are very unlikely to end up in the same memory position when you restart the game (or even change to another level, or even during gameplay). So, if you perform a scan and find that the address where your health is stored is &lt;code&gt;0x73AABABE&lt;&#x2F;code&gt;, you might be tempted to save it and reuse it next time you launch the game. Now you don&#x27;t need to scan for it again! Alas, as soon as you restart the game, the health is now stored at &lt;code&gt;0x5AADBEEF&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
  27&lt;p&gt;Not all hope is lost! The game must &lt;em&gt;somehow&lt;&#x2F;em&gt; have a way to reliably find this value, and the way it&#x27;s done is with pointers. There will always be some base address that holds a pointer, and the game code knows where to find this pointer. If we are also able to find the pointer at said base address, and follow it ourselves (&amp;quot;dereferencing&amp;quot; it), we can perform the same steps the game is doing, and reliably find the health no matter how much we restart the game&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;.&lt;&#x2F;p&gt;
  28&lt;h2 id=&quot;code-finder&quot;&gt;Code finder&lt;&#x2F;h2&gt;
  29&lt;details open&gt;&lt;summary&gt;Cheat Engine Tutorial: Step 6&lt;&#x2F;summary&gt;
  30&lt;blockquote&gt;
  31&lt;p&gt;In the previous step I explained how to use the Code finder to handle changing locations. But that method alone makes it difficult to find the address to set the values you want. That&#x27;s why there are pointers:&lt;&#x2F;p&gt;
  32&lt;p&gt;At the bottom you&#x27;ll find 2 buttons. One will change the value, and the other changes the value AND the location of the value. For this step you don&#x27;t really need to know assembler, but it helps a lot if you do.&lt;&#x2F;p&gt;
  33&lt;p&gt;First find the address of the value. When you&#x27;ve found it use the function to find out what accesses this address.&lt;&#x2F;p&gt;
  34&lt;p&gt;Change the value again, and a item will show in the list. Double click that item. (or select and click on more info) and a new window will open with detailed information on what happened when the instruction ran.&lt;&#x2F;p&gt;
  35&lt;p&gt;If the assembler instruction doesn&#x27;t have anything between a &#x27;[&#x27; and &#x27;]&#x27; then use another item in the list. If it does it will say what it think will be the value of the pointer you need.&lt;&#x2F;p&gt;
  36&lt;p&gt;Go back to the main cheat engine window (you can keep this extra info window open if you want, but if you close it, remember what is between the [ and ]) and do a 4 byte scan in hexadecimal for the value the extra info told you. When done scanning it may return 1 or a few hundred addresses. Most of the time the address you need will be the smallest one. Now click on manually add and select the pointer checkbox.&lt;&#x2F;p&gt;
  37&lt;p&gt;The window will change and allow you to type in the address of a pointer and a offset. Fill in as address the address you just found. If the assembler instruction has a calculation (e.g: [esi+12]) at the end then type the value in that&#x27;s at the end. else leave it 0. If it was a more complicated instruction look at the calculation.&lt;&#x2F;p&gt;
  38&lt;p&gt;Example of a more complicated instruction:&lt;&#x2F;p&gt;
  39&lt;p&gt;[EAX*2+EDX+00000310] eax=4C and edx=00801234.&lt;&#x2F;p&gt;
  40&lt;p&gt;In this case EDX would be the value the pointer has, and EAX*2+00000310 the offset, so the offset you&#x27;d fill in would be 2*4C+00000310=3A8.  (this is all in hex, use calc.exe from windows in scientific mode to calculate).&lt;&#x2F;p&gt;
  41&lt;p&gt;Back to the tutorial, click OK and the address will be added, If all went right the address will show P-&amp;gt;xxxxxxx, with xxxxxxx being the address of the value you found. If thats not right, you&#x27;ve done something wrong. Now, change the value using the pointer you added in 5000 and freeze it. Then click Change pointer, and if all went right the next button will become visible.&lt;&#x2F;p&gt;
  42&lt;p&gt;&lt;em&gt;extra&lt;&#x2F;em&gt;: And you could also use the pointer scanner to find the pointer to this address.&lt;&#x2F;p&gt;
  43&lt;&#x2F;blockquote&gt;
  44&lt;&#x2F;details&gt;
  45&lt;h2 id=&quot;on-access-watchpoints&quot;&gt;On-access watchpoints&lt;&#x2F;h2&gt;
  46&lt;p&gt;Last time we managed to learn how hardware breakpoints were being set by observing Cheat Engine&#x27;s behaviour. I think it&#x27;s now time to handle this properly instead. We&#x27;ll check out the &lt;a href=&quot;https:&#x2F;&#x2F;wiki.osdev.org&#x2F;CPU_Registers_x86#Debug_Registers&quot;&gt;CPU Registers x86 page on OSDev&lt;&#x2F;a&gt; to learn about it:&lt;&#x2F;p&gt;
  47&lt;ul&gt;
  48&lt;li&gt;DR0, DR1, DR2 and DR3 can hold a memory address each. This address will be used by the breakpoint.&lt;&#x2F;li&gt;
  49&lt;li&gt;DR4 is actually an &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;X86_debug_register&quot;&gt;obsolete synonym&lt;&#x2F;a&gt; for DR6.&lt;&#x2F;li&gt;
  50&lt;li&gt;DR5 is another obsolete synonym, this time for DR7.&lt;&#x2F;li&gt;
  51&lt;li&gt;DR6 is debug status. The four lowest bits indicate which breakpoint was hit, and the four highest bits contain additional information. We should make sure to clear this ourselves when a breakpoint is hit.&lt;&#x2F;li&gt;
  52&lt;li&gt;DR7 is debug control, which we need to study more carefully.&lt;&#x2F;li&gt;
  53&lt;&#x2F;ul&gt;
  54&lt;p&gt;Each debug register DR0 through DR3 has two corresponding bits in DR7, starting from the lowest-order bit, to indicate whether the corresponding register is a &lt;strong&gt;L&lt;&#x2F;strong&gt;ocal or &lt;strong&gt;G&lt;&#x2F;strong&gt;lobal breakpoint. So it looks like this:&lt;&#x2F;p&gt;
  55&lt;pre&gt;&lt;code&gt;  Meaning: [ .. .. | G3 | L3 | G2 | L2 | G1 | L1 | G0 | L0 ]
  56Bit-index:   31-08 | 07 | 06 | 05 | 04 | 03 | 02 | 01 | 00
  57&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
  58&lt;p&gt;Cheat Engine was using local breakpoints, because the zeroth bit was set. Probably because we don&#x27;t want these breakpoints to infect other programs! Because we were using only one breakpoint, only the lowermost bit was being set. The local 1st, 2nd and 3rd bits were unset.&lt;&#x2F;p&gt;
  59&lt;p&gt;Now, each debug register DR0 through DR4 has four additional bits in DR7, two for the &lt;strong&gt;C&lt;&#x2F;strong&gt;ondition and another two for the &lt;strong&gt;S&lt;&#x2F;strong&gt;ize:&lt;&#x2F;p&gt;
  60&lt;pre&gt;&lt;code&gt;  Meaning: [   S3  |   C3  |   S2  |   C2  |   S1  |   C1  |   S0  |   C0  | .. .. ]
  61Bit-index:   31 30 | 29 28 | 27 26 | 25 24 | 23 22 | 21 20 | 19 18 | 17 16 | 15-00
  62&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
  63&lt;p&gt;The two bits of the condition mean the following:&lt;&#x2F;p&gt;
  64&lt;ul&gt;
  65&lt;li&gt;&lt;code&gt;00&lt;&#x2F;code&gt; execution breakpoint.&lt;&#x2F;li&gt;
  66&lt;li&gt;&lt;code&gt;01&lt;&#x2F;code&gt; write watchpoint.&lt;&#x2F;li&gt;
  67&lt;li&gt;&lt;code&gt;11&lt;&#x2F;code&gt; read&#x2F;write watchpoint.&lt;&#x2F;li&gt;
  68&lt;li&gt;&lt;code&gt;10&lt;&#x2F;code&gt; unsupported I&#x2F;O read&#x2F;write.&lt;&#x2F;li&gt;
  69&lt;&#x2F;ul&gt;
  70&lt;p&gt;When we were using Cheat Engine to add write watchpoints, the bits 17 and 16 were indeed set to &lt;code&gt;01&lt;&#x2F;code&gt;, and the bits 19 and 18 were set to &lt;code&gt;11&lt;&#x2F;code&gt;. Hm, but &lt;em&gt;11&lt;sub&gt;2&lt;&#x2F;sub&gt; = 3&lt;sub&gt;10&lt;&#x2F;sub&gt;&lt;&#x2F;em&gt; , and yet, we were watching writes to 4 bytes. So what&#x27;s up with this? Is there a different mapping for the size which isn&#x27;t documented at the time of writing? Seems we need to learn from Cheat Engine&#x27;s behaviour one more time.&lt;&#x2F;p&gt;
  71&lt;p&gt;For reference, this is what DR7 looked like when we added a single write watchpoint:&lt;&#x2F;p&gt;
  72&lt;pre&gt;&lt;code&gt;hex: 000d_0001
  73bin: 00000000_00001101_00000000_00000001
  74&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
  75&lt;p&gt;And this is the code I will be using to check the breakpoints of different sizes:&lt;&#x2F;p&gt;
  76&lt;pre&gt;&lt;code&gt;thread::enum_threads(pid)
  77    .unwrap()
  78    .into_iter()
  79    .for_each(|tid| {
  80        let thread = thread::Thread::open(tid).unwrap();
  81        let ctx = thread.get_context().unwrap();
  82        eprintln!(&amp;quot;hex: {:08x}&amp;quot;, ctx.Dr7);
  83        eprintln!(&amp;quot;bin: {:032b}&amp;quot;, ctx.Dr7);
  84    });
  85&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
  86&lt;p&gt;Let&#x27;s compare this to watchpoints for sizes 1, 2, 4 and 8 bytes:&lt;&#x2F;p&gt;
  87&lt;pre&gt;&lt;code&gt;1 byte
  88hex: 0001_0401
  89bin: 00000000_00000001_00000100_00000001
  90
  912 bytes
  92hex: 0005_0401
  93bin: 00000000_00000101_00000100_00000001
  94
  954 bytes
  96hex: 000d_0401
  97bin: 00000000_00001101_00000100_00000001
  98
  998 bytes
 100hex: 0009_0401
 101bin: 00000000_00001001_00000100_00000001
 102                            ^ wut?
 103&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 104&lt;p&gt;I have no idea what&#x27;s up with that stray tenth bit. Its use does not seem documented, and things worked fine without it, so we&#x27;ll ignore it. The lowest bit is set to indicate we&#x27;re using DR0, bits 17 and 16 represent the write watchpoint, and the size seems to be as follows:&lt;&#x2F;p&gt;
 105&lt;ul&gt;
 106&lt;li&gt;&lt;code&gt;00&lt;&#x2F;code&gt; for a single byte.&lt;&#x2F;li&gt;
 107&lt;li&gt;&lt;code&gt;01&lt;&#x2F;code&gt; for two bytes (a &amp;quot;word&amp;quot;).&lt;&#x2F;li&gt;
 108&lt;li&gt;&lt;code&gt;11&lt;&#x2F;code&gt; for four bytes (a &amp;quot;double word&amp;quot;).&lt;&#x2F;li&gt;
 109&lt;li&gt;&lt;code&gt;10&lt;&#x2F;code&gt; for eight bytes (a &amp;quot;quadruple word&amp;quot;).&lt;&#x2F;li&gt;
 110&lt;&#x2F;ul&gt;
 111&lt;p&gt;Doesn&#x27;t make much sense if you ask me, but we&#x27;ll roll with it. Just to confirm, this is what the &amp;quot;on-access&amp;quot; breakpoint looks like according to Cheat Engine:&lt;&#x2F;p&gt;
 112&lt;pre&gt;&lt;code&gt;hex: 000f_0401
 113bin: 00000000_00001111_00000100_00000001
 114&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 115&lt;p&gt;So it all checks out! The bit pattern is &lt;code&gt;11&lt;&#x2F;code&gt; for read&#x2F;write (technically, a write is also an access). Let&#x27;s implement this!&lt;&#x2F;p&gt;
 116&lt;h2 id=&quot;proper-breakpoint-handling&quot;&gt;Proper breakpoint handling&lt;&#x2F;h2&gt;
 117&lt;p&gt;The first thing we need to do is represent the possible breakpoint conditions:&lt;&#x2F;p&gt;
 118&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;#[repr(u8)]
 119pub enum Condition {
 120    Execute = 0b00,
 121    Write = 0b01,
 122    Access = 0b11,
 123}
 124&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 125&lt;p&gt;And also the legal breakpoint sizes:&lt;&#x2F;p&gt;
 126&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;#[repr(u8)]
 127pub enum Size {
 128    Byte = 0b00,
 129    Word = 0b01,
 130    DoubleWord = 0b11,
 131    QuadWord = 0b10,
 132}
 133&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 134&lt;p&gt;We are using &lt;code&gt;#[repr(u8)]&lt;&#x2F;code&gt; so that we can convert a given variant into the corresponding bit pattern. With the right types defined in order to set a breakpoint, we can start implementing the method that will set them (inside &lt;code&gt;impl Thread&lt;&#x2F;code&gt;):&lt;&#x2F;p&gt;
 135&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub fn add_breakpoint(&amp;amp;self, addr: usize, cond: Condition, size: Size) -&amp;gt; io::Result&amp;lt;Breakpoint&amp;gt; {
 136    let mut context = self.get_context()?;
 137    todo!()
 138}
 139&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 140&lt;p&gt;First, let&#x27;s try finding an &amp;quot;open spot&amp;quot; where we could set our breakpoint. We will &amp;quot;slide&amp;quot; a the &lt;code&gt;0b11&lt;&#x2F;code&gt; bitmask over the lowest eight bits, and if and only if both the local and global bits are unset, then we&#x27;re free to set a breakpoint at this index&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#4&quot;&gt;4&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;:&lt;&#x2F;p&gt;
 141&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let index = (0..4)
 142    .find_map(|i| ((context.Dr7 &amp;amp; (0b11 &amp;lt;&amp;lt; (i * 2))) == 0).then(|| i))
 143    .ok_or_else(|| io::Error::new(io::ErrorKind::Other, &amp;quot;no debug register available&amp;quot;))?;
 144&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 145&lt;p&gt;Once an &lt;code&gt;index&lt;&#x2F;code&gt; is found, we can set the address we want to watch in the corresponding register and update the debug control bits:&lt;&#x2F;p&gt;
 146&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let addr = addr as u64;
 147match index {
 148    0 =&amp;gt; context.Dr0 = addr,
 149    1 =&amp;gt; context.Dr1 = addr,
 150    2 =&amp;gt; context.Dr2 = addr,
 151    3 =&amp;gt; context.Dr3 = addr,
 152    _ =&amp;gt; unreachable!(),
 153}
 154
 155let clear_mask = !((0b1111 &amp;lt;&amp;lt; (16 + index * 4)) | (0b11 &amp;lt;&amp;lt; (index * 2)));
 156context.Dr7 &amp;amp;= clear_mask;
 157
 158context.Dr7 |= 1 &amp;lt;&amp;lt; (index * 2);
 159
 160let sc = (((size as u8) &amp;lt;&amp;lt; 2) | (cond as u8)) as u64;
 161context.Dr7 |= sc &amp;lt;&amp;lt; (16 + index * 4);
 162
 163self.set_context(&amp;amp;context)?;
 164Ok(Breakpoint {
 165    thread: self,
 166    clear_mask,
 167})
 168&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 169&lt;p&gt;Note that we&#x27;re first creating a &amp;quot;clear mask&amp;quot;. We switch on all the bits that we may use for this breakpoint, and then negate. Effectively, &lt;code&gt;Dr7 &amp;amp; clear_mask&lt;&#x2F;code&gt; will make sure we don&#x27;t leave any bit high on accident. We apply the mask before OR-ing the rest of bits to also clear any potential garbage on the size and condition bits. Next, we set the bit to enable the new local breakpoint, and also store the size and condition bits at the right location.&lt;&#x2F;p&gt;
 170&lt;p&gt;With the context updated, we can set it back and return the &lt;code&gt;Breakpoint&lt;&#x2F;code&gt;. It stores the &lt;code&gt;thread&lt;&#x2F;code&gt; and the &lt;code&gt;clear_mask&lt;&#x2F;code&gt; so that it can clean up on &lt;code&gt;Drop&lt;&#x2F;code&gt;. We are technically relying on &lt;code&gt;Drop&lt;&#x2F;code&gt; to run behaviour here, but the cleanup is done on a best-effort basis. If the user intentionally forgets the &lt;code&gt;Breakpoint&lt;&#x2F;code&gt;, maybe they want the &lt;code&gt;Breakpoint&lt;&#x2F;code&gt; to forever be set.&lt;&#x2F;p&gt;
 171&lt;p&gt;This logic is begging for a testcase though; I&#x27;ll split it into a new &lt;code&gt;Breakpoint::update_dbg_control&lt;&#x2F;code&gt; method and test that out:&lt;&#x2F;p&gt;
 172&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;
 173#[cfg(test)]
 174mod tests {
 175    use super::*;
 176
 177    #[test]
 178    fn brk_add_one() {
 179        &#x2F;&#x2F; DR7 starts with garbage which should be respected.
 180        let (clear_mask, dr, dr7) =
 181            Breakpoint::update_dbg_control(0x1700, Condition::Write, Size::DoubleWord).unwrap();
 182
 183        assert_eq!(clear_mask, 0xffff_ffff_fff0_fffc);
 184        assert_eq!(dr, DebugRegister::Dr0);
 185        assert_eq!(dr7, 0x0000_0000_000d_1701);
 186    }
 187
 188    #[test]
 189    fn brk_add_two() {
 190        let (clear_mask, dr, dr7) = Breakpoint::update_dbg_control(
 191            0x0000_0000_000d_0001,
 192            Condition::Write,
 193            Size::DoubleWord,
 194        )
 195        .unwrap();
 196
 197        assert_eq!(clear_mask, 0xffff_ffff_ff0f_fff3);
 198        assert_eq!(dr, DebugRegister::Dr1);
 199        assert_eq!(dr7, 0x0000_0000_00dd_0005);
 200    }
 201
 202    #[test]
 203    fn brk_try_add_when_max() {
 204        assert!(Breakpoint::update_dbg_control(
 205            0x0000_0000_dddd_0055,
 206            Condition::Write,
 207            Size::DoubleWord
 208        )
 209        .is_none());
 210    }
 211}
 212&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 213&lt;pre&gt;&lt;code&gt;running 3 tests
 214test thread::tests::brk_add_one ... ok
 215test thread::tests::brk_add_two ... ok
 216test thread::tests::brk_try_add_when_max ... ok
 217&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 218&lt;p&gt;Very good! With proper breakpoint handling usable, we can continue.&lt;&#x2F;p&gt;
 219&lt;h2 id=&quot;inferring-the-pointer-value&quot;&gt;Inferring the pointer value&lt;&#x2F;h2&gt;
 220&lt;p&gt;After scanning memory for the location we&#x27;re looking for (say, our current health), we then add an access watchpoint, and wait for an exception to occur. As a reminder, here&#x27;s the page with the &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;debug&#x2F;debugging-events&quot;&gt;Debugging Events&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
 221&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let addr = ...;
 222let mut threads = ...;
 223
 224let _watchpoints = threads
 225    .iter_mut()
 226    .map(|thread| {
 227        thread
 228            .add_breakpoint(addr, thread::Condition::Access, thread::Size::DoubleWord)
 229            .unwrap()
 230    })
 231    .collect::&amp;lt;Vec&amp;lt;_&amp;gt;&amp;gt;();
 232
 233loop {
 234    let event = debugger.wait_event(None).unwrap();
 235    if event.dwDebugEventCode == winapi::um::minwinbase::EXCEPTION_DEBUG_EVENT {
 236        let exc = unsafe { event.u.Exception() };
 237        if exc.ExceptionRecord.ExceptionCode == winapi::um::minwinbase::EXCEPTION_SINGLE_STEP {
 238            todo!();
 239        }
 240    }
 241    debugger.cont(event, true).unwrap();
 242}
 243&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 244&lt;p&gt;Now, inside the &lt;code&gt;todo!()&lt;&#x2F;code&gt; we will want to do a few things, namely printing out the instructions &amp;quot;around this location&amp;quot; and dumping the entire thread context on screen. To print the instructions, we need to import &lt;code&gt;iced_x86&lt;&#x2F;code&gt; again, iterate over all memory regions to find the region where the exception happened, read the corresponding bytes, decode the instructions, and when we find the one with a corresponding instruction pointer, print &amp;quot;around it&amp;quot;:&lt;&#x2F;p&gt;
 245&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;use iced_x86::{Decoder, DecoderOptions, Formatter, Instruction, NasmFormatter};
 246
 247let addr = exc.ExceptionRecord.ExceptionAddress as usize;
 248let region = process
 249    .memory_regions()
 250    .into_iter()
 251    .find(|region| {
 252        let base = region.BaseAddress as usize;
 253        base &amp;lt;= addr &amp;amp;&amp;amp; addr &amp;lt; base + region.RegionSize
 254    })
 255    .unwrap();
 256
 257let bytes = process
 258    .read_memory(region.BaseAddress as usize, region.RegionSize)
 259    .unwrap();
 260
 261let mut decoder = Decoder::new(64, &amp;amp;bytes, DecoderOptions::NONE);
 262decoder.set_ip(region.BaseAddress as _);
 263
 264let mut formatter = NasmFormatter::new();
 265let mut output = String::new();
 266
 267let instructions = decoder.into_iter().collect::&amp;lt;Vec&amp;lt;_&amp;gt;&amp;gt;();
 268for (i, ins) in instructions.iter().enumerate() {
 269    if ins.next_ip() as usize == addr {
 270        let low = i.saturating_sub(5);
 271        let high = (i + 5).min(instructions.len());
 272        for j in low..high {
 273            let ins = &amp;amp;instructions[j];
 274            print!(&amp;quot;{} {:016X} &amp;quot;, if j == i { &amp;quot;&amp;gt;&amp;gt;&amp;gt;&amp;quot; } else { &amp;quot;   &amp;quot; }, ins.ip());
 275            let k = (ins.ip() - region.BaseAddress as usize as u64) as usize;
 276            let instr_bytes = &amp;amp;bytes[k..k + ins.len()];
 277            for b in instr_bytes.iter() {
 278                print!(&amp;quot;{:02X}&amp;quot;, b);
 279            }
 280            if instr_bytes.len() &amp;lt; 10 {
 281                for _ in 0..10usize.saturating_sub(instr_bytes.len()) {
 282                    print!(&amp;quot;  &amp;quot;);
 283                }
 284            }
 285
 286            output.clear();
 287            formatter.format(ins, &amp;amp;mut output);
 288            println!(&amp;quot; {}&amp;quot;, output);
 289        }
 290        break;
 291    }
 292}
 293debugger.cont(event, true).unwrap();
 294break;
 295&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 296&lt;p&gt;The result is pretty fancy:&lt;&#x2F;p&gt;
 297&lt;pre&gt;&lt;code&gt;    000000010002CAAC 48894DF0             mov [rbp-10h],rcx
 298    000000010002CAB0 488955F8             mov [rbp-8],rdx
 299    000000010002CAB4 48C745D800000000     mov qword [rbp-28h],0
 300    000000010002CABC 90                   nop
 301    000000010002CABD 488B050CA02D00       mov rax,[rel 100306AD0h]
 302&amp;gt;&amp;gt;&amp;gt; 000000010002CAC4 8B00                 mov eax,[rax]
 303    000000010002CAC6 8945EC               mov [rbp-14h],eax
 304    000000010002CAC9 B9E8030000           mov ecx,3E8h
 305    000000010002CACE E88D2FFEFF           call 000000010000FA60h
 306    000000010002CAD3 8945E8               mov [rbp-18h],eax
 307&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 308&lt;p&gt;Cool! So &lt;code&gt;rax&lt;&#x2F;code&gt; is holding an address, meaning it&#x27;s a pointer, and the value it reads (dereferences) is stored back into &lt;code&gt;eax&lt;&#x2F;code&gt; (because it does not need &lt;code&gt;rax&lt;&#x2F;code&gt; anymore). Alas, the current thread context has the register state &lt;em&gt;after&lt;&#x2F;em&gt; the instruction was executed, and &lt;code&gt;rax&lt;&#x2F;code&gt; no longer contains the address at this point. However, notice how the previous instruction writes a fixed value to &lt;code&gt;rax&lt;&#x2F;code&gt;, and then that value is used to access memory, like so:&lt;&#x2F;p&gt;
 309&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let eax = memory[memory[0x100306AD0]];
 310&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 311&lt;p&gt;The value at &lt;code&gt;memory[0x100306AD0]&lt;&#x2F;code&gt; &lt;em&gt;is&lt;&#x2F;em&gt; the pointer! No offsets are used, because nothing is added to the pointer after it&#x27;s read. This means that, if we simply scan for the address we were looking for, we should find out where the pointer is stored:&lt;&#x2F;p&gt;
 312&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let addr = ...;
 313let scan = process.scan_regions(&amp;amp;regions, Scan::Exact(addr as u64));
 314
 315scan.into_iter().for_each(|region| {
 316    region.locations.iter().for_each(|ptr_addr| {
 317        println!(&amp;quot;[{:x}] = {:x}&amp;quot;, ptr_addr, addr);
 318    });
 319});
 320&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 321&lt;p&gt;And just like that:&lt;&#x2F;p&gt;
 322&lt;pre&gt;&lt;code&gt;[100306ad0] = 15de9f0
 323&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 324&lt;p&gt;Notice how the pointer address found matches with the offset used by the instructions:&lt;&#x2F;p&gt;
 325&lt;pre&gt;&lt;code&gt;    000000010002CABD 488B050CA02D00       mov rax,[rel 100306AD0h]
 326           this is the same as the value we just found ^^^^^^^^^^
 327&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 328&lt;p&gt;Very interesting indeed. We were actually very lucky to have only found a single memory location containing the pointer value, &lt;code&gt;0x15de9f0&lt;&#x2F;code&gt;. Cheat Engine somehow knows that this value is always stored at &lt;code&gt;0x100306ad0&lt;&#x2F;code&gt; (or rather, at &lt;code&gt;Tutorial-x86_64.exe+306AD0&lt;&#x2F;code&gt;), because the address shows green. How does it do this?&lt;&#x2F;p&gt;
 329&lt;h2 id=&quot;base-addresses&quot;&gt;Base addresses&lt;&#x2F;h2&gt;
 330&lt;p&gt;Remember back in &lt;a href=&quot;&#x2F;blog&#x2F;woce-2&quot;&gt;part 2&lt;&#x2F;a&gt; when we introduced the memory regions? They&#x27;re making a comeback! A memory region contains both the current memory protection option &lt;em&gt;and&lt;&#x2F;em&gt; the protection level when the region was created. If we try printing out the protection levels for both the memory region containing the value, and the memory region containing the pointer, this is what we get (the addresses differ from the ones previously because I restarted the tutorial):&lt;&#x2F;p&gt;
 331&lt;pre&gt;&lt;code&gt;Region holding the value:
 332    BaseAddress: 0xb0000
 333    AllocationBase: 0xb0000
 334    AllocationProtect: 0x4
 335    RegionSize: 1007616
 336    State: 4096
 337    Protect: 4
 338    Type: 0x20000
 339
 340Region holding the pointer:
 341    BaseAddress: 0x100304000
 342    AllocationBase: 0x100000000
 343    AllocationProtect: 0x80
 344    RegionSize: 28672
 345    State: 4096
 346    Protect: 4
 347    Type: 0x1000000
 348&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 349&lt;p&gt;Interesting! According to the &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;winnt&#x2F;ns-winnt-memory_basic_information&quot;&gt;&lt;code&gt;MEMORY_BASIC_INFORMATION&lt;&#x2F;code&gt; page&lt;&#x2F;a&gt;, the type for the first region is &lt;code&gt;MEM_PRIVATE&lt;&#x2F;code&gt;, and the type for the second region is &lt;code&gt;MEM_IMAGE&lt;&#x2F;code&gt; which:&lt;&#x2F;p&gt;
 350&lt;blockquote&gt;
 351&lt;p&gt;Indicates that the memory pages within the region are mapped into the view of an image section.&lt;&#x2F;p&gt;
 352&lt;&#x2F;blockquote&gt;
 353&lt;p&gt;The protection also changes from &lt;code&gt;PAGE_EXECUTE_WRITECOPY&lt;&#x2F;code&gt; to simply &lt;code&gt;PAGE_READWRITE&lt;&#x2F;code&gt;, but I don&#x27;t think it&#x27;s relevant. Neither the type seems to be much more relevant. In &lt;a href=&quot;&#x2F;blog&#x2F;woce-2&quot;&gt;part 2&lt;&#x2F;a&gt; we also mentioned the concept of &amp;quot;base address&amp;quot;, but decided against using it, because starting to look for regions at address zero seemed to work fine. However, it would make sense that fixed &amp;quot;addresses&amp;quot; start at some known &amp;quot;base&amp;quot;. Let&#x27;s try getting the &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;26573045&#x2F;4759433&quot;&gt;base address for all loaded modules&lt;&#x2F;a&gt;. Currently, we only get the address for the base module, in order to retrieve its name, but now we need them all:&lt;&#x2F;p&gt;
 354&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub fn enum_modules(&amp;amp;self) -&amp;gt; io::Result&amp;lt;Vec&amp;lt;winapi::shared::minwindef::HMODULE&amp;gt;&amp;gt; {
 355    let mut size = 0;
 356    if unsafe {
 357        winapi::um::psapi::EnumProcessModules(
 358            self.handle.as_ptr(),
 359            ptr::null_mut(),
 360            0,
 361            &amp;amp;mut size,
 362        )
 363    } == FALSE
 364    {
 365        return Err(io::Error::last_os_error());
 366    }
 367
 368    let mut modules = Vec::with_capacity(size as usize &#x2F; mem::size_of::&amp;lt;HMODULE&amp;gt;());
 369    if unsafe {
 370        winapi::um::psapi::EnumProcessModules(
 371            self.handle.as_ptr(),
 372            modules.as_mut_ptr(),
 373            (modules.capacity() * mem::size_of::&amp;lt;HMODULE&amp;gt;()) as u32,
 374            &amp;amp;mut size,
 375        )
 376    } == FALSE
 377    {
 378        return Err(io::Error::last_os_error());
 379    }
 380
 381    unsafe {
 382        modules.set_len(size as usize &#x2F; mem::size_of::&amp;lt;HMODULE&amp;gt;());
 383    }
 384
 385    Ok(modules)
 386}
 387&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 388&lt;p&gt;The first call is used to retrieve the correct &lt;code&gt;size&lt;&#x2F;code&gt;, then we allocate just enough, and make the second call. The returned type are pretty much memory addresses, so let&#x27;s see if we can find regions that contain them:&lt;&#x2F;p&gt;
 389&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let mut bases = 0;
 390let modules = process.enum_modules().unwrap();
 391let regions = process.memory_regions();
 392regions.iter().for_each(|region| {
 393    if modules.iter().any(|module| {
 394        let base = region.AllocationBase as usize;
 395        let addr = *module as usize;
 396        base &amp;lt;= addr &amp;amp;&amp;amp; addr &amp;lt; base + region.RegionSize
 397    }) {
 398        bases += 1;
 399    }
 400});
 401
 402println!(
 403    &amp;quot;{}&#x2F;{} regions have a module address within them&amp;quot;,
 404    bases,
 405    regions.len()
 406);
 407&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 408&lt;pre&gt;&lt;code&gt;41&#x2F;353 regions have a module address within them
 409&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 410&lt;p&gt;Exciting stuff! It appears &lt;code&gt;base == addr&lt;&#x2F;code&gt; also does the trick&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#5&quot;&gt;5&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;, so now we could build a &lt;code&gt;bases: HashSet&amp;lt;usize&amp;gt;&lt;&#x2F;code&gt; and simply check if &lt;code&gt;bases.contains(&amp;amp;region.AllocationBase as usize)&lt;&#x2F;code&gt; to determine whether &lt;code&gt;region&lt;&#x2F;code&gt; is a base address or not&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#6&quot;&gt;6&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. So there we have it! The address holding the pointer value does fall within one of these &amp;quot;base regions&amp;quot;. You can also get the name from one of these module addresses, and print it in the same way as Cheat Engine does it (such as &lt;code&gt;Tutorial-x86_64.exe+306AD0&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
 411&lt;h2 id=&quot;finale&quot;&gt;Finale&lt;&#x2F;h2&gt;
 412&lt;p&gt;So, there&#x27;s no &amp;quot;automated&amp;quot; solution to all of this? That&#x27;s the end? Well, yes, once you have a pointer you can dereference it once and then write to the given address to complete the tutorial step! I can understand how this would feel a bit underwhelming, but in all fairness, we were required to pretty-print assembly to guess what pointer address we could potentially need to look for. There is an &lt;a href=&quot;https:&#x2F;&#x2F;www.intel.com&#x2F;content&#x2F;www&#x2F;us&#x2F;en&#x2F;architecture-and-technology&#x2F;64-ia-32-architectures-software-developer-vol-2a-manual.html&quot;&gt;stupidly large amount of instructions&lt;&#x2F;a&gt;, and I&#x27;m sure a lot of them can access memory, so automating that would be rough. We were lucky that the instructions right before the one that hit the breakpoint were changing the memory address, but you could imagine this value coming from somewhere completely different. It could also be using a myriad of different techniques to apply the offset. I would argue manual intervention is a must here&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#7&quot;&gt;7&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;.&lt;&#x2F;p&gt;
 413&lt;p&gt;We have learnt how to pretty-print instructions, and had a very gentle introduction to figuring out what we may need to look for. The code to retrieve the loaded modules, and their corresponding regions, will come in handy later on. Having access to this information lets us know when to stop looking for additional pointers. As soon as a pointer is found within a memory region corresponding to a base module, we&#x27;re done! Also, I know the title doesn&#x27;t really much the contents of this entry (sorry about that), but I&#x27;m just following the convention of calling it whatever the Cheat Engine tutorial calls them.&lt;&#x2F;p&gt;
 414&lt;p&gt;The &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lonami&#x2F;memo&quot;&gt;code for this post&lt;&#x2F;a&gt; is available over at my GitHub. You can run &lt;code&gt;git checkout step6&lt;&#x2F;code&gt; after cloning the repository to get the right version of the code, although you will have to &lt;code&gt;checkout&lt;&#x2F;code&gt; to individual commits if you want to review, for example, how the instructions were printed out. Only the code necessary to complete the step is included at the &lt;code&gt;step6&lt;&#x2F;code&gt; tag.&lt;&#x2F;p&gt;
 415&lt;p&gt;In the next post, we&#x27;ll tackle the sixth step of the tutorial: Code Injection. This will be pretty similar to part 5, but instead of writing out a simple NOP instruction, we will have to get a bit more creative.&lt;&#x2F;p&gt;
 416&lt;h3 id=&quot;footnotes&quot;&gt;Footnotes&lt;&#x2F;h3&gt;
 417&lt;div class=&quot;footnote-definition&quot; id=&quot;1&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;&#x2F;sup&gt;
 418&lt;p&gt;This will only be a gentle introduction to pointers. Part 8 of this series will have to rely on more advanced techniques.&lt;&#x2F;p&gt;
 419&lt;&#x2F;div&gt;
 420&lt;div class=&quot;footnote-definition&quot; id=&quot;2&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;2&lt;&#x2F;sup&gt;
 421&lt;p&gt;Kind of. The size of a pointer isn&#x27;t necessarily the size as &lt;code&gt;usize&lt;&#x2F;code&gt;, although &lt;code&gt;usize&lt;&#x2F;code&gt; is guaranteed to be able of representing every possible address. For our purposes, we can assume a pointer is as big as &lt;code&gt;usize&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
 422&lt;&#x2F;div&gt;
 423&lt;div class=&quot;footnote-definition&quot; id=&quot;3&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;3&lt;&#x2F;sup&gt;
 424&lt;p&gt;Game updates are likely to pull more code and shuffle stuff around. This is unfortunately a difficult problem to solve. But storing a pointer which is usable across restarts for as long as the game doesn&#x27;t update is still a pretty darn big improvement over having to constantly scan for the locations we care about. Although if you&#x27;re smart enough to look for certain unique patterns, even if the code is changed, finding those patterns will give you the new updated address, so it&#x27;s not &lt;em&gt;impossible&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
 425&lt;&#x2F;div&gt;
 426&lt;div class=&quot;footnote-definition&quot; id=&quot;4&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;4&lt;&#x2F;sup&gt;
 427&lt;p&gt;&lt;code&gt;bool::then&lt;&#x2F;code&gt; is a pretty recent addition at the time of writing (1.50.0), so make sure you &lt;code&gt;rustup update&lt;&#x2F;code&gt; if it&#x27;s erroring out!&lt;&#x2F;p&gt;
 428&lt;&#x2F;div&gt;
 429&lt;div class=&quot;footnote-definition&quot; id=&quot;5&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;5&lt;&#x2F;sup&gt;
 430&lt;p&gt;I wasn&#x27;t sure if there would be some metadata before the module base address but within the region, so I went with the range check. What &lt;em&gt;is&lt;&#x2F;em&gt; important however is using &lt;code&gt;AllocationBase&lt;&#x2F;code&gt;, not &lt;code&gt;BaseAddress&lt;&#x2F;code&gt;. They&#x27;re different, and this did bite me.&lt;&#x2F;p&gt;
 431&lt;&#x2F;div&gt;
 432&lt;div class=&quot;footnote-definition&quot; id=&quot;6&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;6&lt;&#x2F;sup&gt;
 433&lt;p&gt;As usual, I have no idea if this is how Cheat Engine is doing it, but it seems reasonable.&lt;&#x2F;p&gt;
 434&lt;&#x2F;div&gt;
 435&lt;div class=&quot;footnote-definition&quot; id=&quot;6&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;6&lt;&#x2F;sup&gt;
 436&lt;p&gt;But nothing&#x27;s stopping you from implementing some heuristics to get the job done for you. If you run some algorithm in your head to find what the pointer value could be, you can program it in Rust as well, although I don&#x27;t think it&#x27;s worth the effort.&lt;&#x2F;p&gt;
 437&lt;&#x2F;div&gt;
 438</content>
 439	</entry>
 440	<entry xml:lang="en">
 441		<title>Writing our own Cheat Engine: Code finder</title>
 442		<published>2021-03-06T00:00:00+00:00</published>
 443		<updated>2021-03-06T00:00:00+00:00</updated>
 444		<link href="https://lonami.dev/blog/woce-5/" type="text/html"/>
 445		<id>https://lonami.dev/blog/woce-5/</id>
 446		<content type="html">&lt;p&gt;This is part 5 on the &lt;em&gt;Writing our own Cheat Engine&lt;&#x2F;em&gt; series:&lt;&#x2F;p&gt;
 447&lt;ul&gt;
 448&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-1&quot;&gt;Part 1: Introduction&lt;&#x2F;a&gt; (start here if you&#x27;re new to the series!)&lt;&#x2F;li&gt;
 449&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-2&quot;&gt;Part 2: Exact Value scanning&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
 450&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-3&quot;&gt;Part 3: Unknown initial value&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
 451&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-4&quot;&gt;Part 4: Floating points&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
 452&lt;li&gt;Part 5: Code finder&lt;&#x2F;li&gt;
 453&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-6&quot;&gt;Part 6: Pointers&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
 454&lt;&#x2F;ul&gt;
 455&lt;p&gt;In part 4 we spent a good deal of time trying to make our scans generic, and now we have something that works&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;! Now that the scanning is fairly powerful and all covered, the Cheat Engine tutorial shifts focus into slightly more advanced techniques that you will most certainly need in anything bigger than a toy program.&lt;&#x2F;p&gt;
 456&lt;p&gt;It&#x27;s time to write our very own &lt;strong&gt;debugger&lt;&#x2F;strong&gt; in Rust!&lt;&#x2F;p&gt;
 457&lt;h2 id=&quot;code-finder&quot;&gt;Code finder&lt;&#x2F;h2&gt;
 458&lt;details open&gt;&lt;summary&gt;Cheat Engine Tutorial: Step 5&lt;&#x2F;summary&gt;
 459&lt;blockquote&gt;
 460&lt;p&gt;Sometimes the location something is stored at changes when you restart the game, or even while you&#x27;re playing… In that case you can use 2 things to still make a table that works. In this step I&#x27;ll try to describe how to use the Code Finder function.&lt;&#x2F;p&gt;
 461&lt;p&gt;The value down here will be at a different location each time you start the tutorial, so a normal entry in the address list wouldn&#x27;t work. First try to find the address. (You&#x27;ve got to this point so I assume you know how to.)&lt;&#x2F;p&gt;
 462&lt;p&gt;When you&#x27;ve found the address, right-click the address in Cheat Engine and choose &amp;quot;Find out what writes to this address&amp;quot;. A window will pop up with an empty list.&lt;&#x2F;p&gt;
 463&lt;p&gt;Then click on the Change value button in this tutorial, and go back to Cheat Engine. If everything went right there should be an address with assembler code there now.&lt;&#x2F;p&gt;
 464&lt;p&gt;Click it and choose the replace option to replace it with code that does nothing. That will also add the code address to the code list in the advanced options window. (Which gets saved if you save your table.)&lt;&#x2F;p&gt;
 465&lt;p&gt;Click on stop, so the game will start running normal again, and close to close the window. Now, click on Change value, and if everything went right the Next button should become enabled.&lt;&#x2F;p&gt;
 466&lt;p&gt;Note: When you&#x27;re freezing the address with a high enough speed it may happen that next becomes visible anyhow&lt;&#x2F;p&gt;
 467&lt;&#x2F;blockquote&gt;
 468&lt;&#x2F;details&gt;
 469&lt;h2 id=&quot;baby-steps-to-debugging&quot;&gt;Baby steps to debugging&lt;&#x2F;h2&gt;
 470&lt;p&gt;Although I have used debuggers before, I have never had a need to write one myself so it&#x27;s time for some research.&lt;&#x2F;p&gt;
 471&lt;p&gt;Searching on DuckDuckGo, I can find entire series to &lt;a href=&quot;http:&#x2F;&#x2F;system.joekain.com&#x2F;debugger&#x2F;&quot;&gt;Writing a Debugger&lt;&#x2F;a&gt;. We would be done by now if only that series wasn&#x27;t written for Linux. The Windows documentation contains a section called &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;debug&#x2F;creating-a-basic-debugger&quot;&gt;Creating a Basic Debugger&lt;&#x2F;a&gt;, but as far as I can tell, it only teaches you the &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;debug&#x2F;debugging-functions&quot;&gt;functions&lt;&#x2F;a&gt; needed to configure the debugging loop. Which mind you, we will need, but in due time.&lt;&#x2F;p&gt;
 472&lt;p&gt;According to &lt;a href=&quot;https:&#x2F;&#x2F;www.gironsec.com&#x2F;blog&#x2F;2013&#x2F;12&#x2F;writing-your-own-debugger-windows-in-c&#x2F;&quot;&gt;Writing your own windows debugger in C&lt;&#x2F;a&gt;, the steps needed to write a debugger are:&lt;&#x2F;p&gt;
 473&lt;ul&gt;
 474&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;processthreadsapi&#x2F;nf-processthreadsapi-suspendthread&quot;&gt;&lt;code&gt;SuspendThread(proc)&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. It makes sense that we need to pause all the threads&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; before messing around with the code the program is executing, or things are very prone to go wrong.&lt;&#x2F;li&gt;
 475&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;processthreadsapi&#x2F;nf-processthreadsapi-getthreadcontext&quot;&gt;&lt;code&gt;GetThreadContext(proc)&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. This function retrieves the appropriate context of the specified thread and is highly processor specific. It basically takes a snapshot of all the registers. Think of registers like extremely fast, but also extremely limited, memory the processor uses.&lt;&#x2F;li&gt;
 476&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;winbase&#x2F;nf-winbase-debugbreakprocess&quot;&gt;&lt;code&gt;DebugBreakProcess&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. Essentially &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;drivers&#x2F;debugger&#x2F;x86-instructions#miscellaneous&quot;&gt;writes out the 0xCC opcode&lt;&#x2F;a&gt;, &lt;code&gt;int 3&lt;&#x2F;code&gt; in assembly, also known as software breakpoint. It&#x27;s written wherever the Register Instruction Pointer (RIP&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;) currently points to, so in essence, when the thread resumes, it will immediately &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;q&#x2F;3915511&#x2F;&quot;&gt;trigger the breakpoint&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
 477&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;debugapi&#x2F;nf-debugapi-continuedebugevent&quot;&gt;&lt;code&gt;ContinueDebugEvent&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. Presumably continues debugging.&lt;&#x2F;li&gt;
 478&lt;&#x2F;ul&gt;
 479&lt;p&gt;There are pages documenting &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;debug&#x2F;debugging-events&quot;&gt;all of the debug events&lt;&#x2F;a&gt; that our debugger will be able to handle.&lt;&#x2F;p&gt;
 480&lt;p&gt;Okay, nice! Software breakpoints seem to be done by writing out memory to the region where the program is reading instructions from. We know how to write memory, as that&#x27;s what all the previous posts have been doing to complete the corresponding tutorial steps. After the breakpoint is executed, all we need to do is &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;q&#x2F;3747852&#x2F;&quot;&gt;restore the original memory back&lt;&#x2F;a&gt; so that the next time the program executes the code it sees no difference.&lt;&#x2F;p&gt;
 481&lt;p&gt;But a software breakpoint will halt execution when the code executes the interrupt instruction. This step of the tutorial wants us to find &lt;em&gt;what writes to a memory location&lt;&#x2F;em&gt;. Where should we place the breakpoint to detect such location? Writing out the instruction to the memory we want to break in won&#x27;t do; it&#x27;s not an instruction, it&#x27;s just data.&lt;&#x2F;p&gt;
 482&lt;p&gt;The name may have given it away. If we&#x27;re talking about software breakpoints, it makes sense that there would exist such a thing as &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Breakpoint#Hardware&quot;&gt;&lt;em&gt;hardware&lt;&#x2F;em&gt; breakpoints&lt;&#x2F;a&gt;. Because they&#x27;re tied to the hardware, they&#x27;re highly processor-specific, but luckily for us, the processor on your usual desktop computer probably has them! Even the &lt;a href=&quot;https:&#x2F;&#x2F;interrupt.memfault.com&#x2F;blog&#x2F;cortex-m-breakpoints&quot;&gt;cortex-m&lt;&#x2F;a&gt; does. The wikipedia page also tells us the name of the thing we&#x27;re looking for, watchpoints:&lt;&#x2F;p&gt;
 483&lt;blockquote&gt;
 484&lt;p&gt;Other kinds of conditions can also be used, such as the reading, writing, or modification of a specific location in an area of memory. This is often referred to as a conditional breakpoint, a data breakpoint, or a watchpoint.&lt;&#x2F;p&gt;
 485&lt;&#x2F;blockquote&gt;
 486&lt;p&gt;A breakpoint that triggers when a specific memory location is written to is exactly what we need, and &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;19109153&#x2F;&quot;&gt;x86 has debug registers D0 to D3 to track memory addresses&lt;&#x2F;a&gt;. As far as I can tell, there is no API in specific to mess with the registers. But we don&#x27;t need any of that! We can just go ahead and &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;unstable-book&#x2F;library-features&#x2F;asm.html&quot;&gt;write some assembly by hand&lt;&#x2F;a&gt; to access these registers. At the time of writing, inline assembly is unstable, so we need a nightly compiler. Run &lt;code&gt;rustup toolchain install nightly&lt;&#x2F;code&gt; if you haven&#x27;t yet, and execute the following code with &lt;code&gt;cargo +nightly run&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
 487&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;#![feature(asm)] &#x2F;&#x2F; top of the file
 488
 489fn main() {
 490    let x: u64 = 123;
 491    unsafe {
 492        asm!(&amp;quot;mov dr7, {}&amp;quot;, in(reg) x);
 493    }
 494}
 495
 496&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 497&lt;p&gt;&lt;code&gt;dr7&lt;&#x2F;code&gt; stands is the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;X86_debug_register&quot;&gt;debug control register&lt;&#x2F;a&gt;, and running this we get…&lt;&#x2F;p&gt;
 498&lt;pre&gt;&lt;code&gt;&amp;gt;cargo +nightly run
 499   Compiling memo v0.1.0
 500    Finished dev [unoptimized + debuginfo] target(s) in 0.74s
 501     Running `target\debug\memo.exe`
 502error: process didn&#x27;t exit successfully: `target\debug\memo.exe` (exit code: 0xc0000096, STATUS_PRIVILEGED_INSTRUCTION)
 503&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 504&lt;p&gt;…an exception! In all fairness, I have no idea what that code would have done. So maybe the &lt;code&gt;STATUS_PRIVILEGED_INSTRUCTION&lt;&#x2F;code&gt; is just trying to protect us. Can we read from the register instead, and see it&#x27;s default value?&lt;&#x2F;p&gt;
 505&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let x: u64;
 506unsafe {
 507    asm!(&amp;quot;mov {}, dr7&amp;quot;, out(reg) x);
 508}
 509assert_eq!(x, 5);
 510&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 511&lt;pre&gt;&lt;code&gt;&amp;gt;cargo +nightly run
 512...
 513error: process didn&#x27;t exit successfully: `target\debug\memo.exe` (exit code: 0xc0000096, STATUS_PRIVILEGED_INSTRUCTION)
 514&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 515&lt;p&gt;Nope. Okay, it seems directly reading from or writing to the debug register is a ring-0 thing. Surely there&#x27;s a way around this. But first we should figure out how to enumerate and pause all the threads.&lt;&#x2F;p&gt;
 516&lt;h2 id=&quot;pausing-all-the-threads&quot;&gt;Pausing all the threads&lt;&#x2F;h2&gt;
 517&lt;p&gt;It seems there is no straightforward way to enumerate the threads. One has to &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;1206915&#x2F;&quot;&gt;create a &amp;quot;toolhelp&amp;quot;&lt;&#x2F;a&gt; and poll the entries. I won&#x27;t bore you with the details. Let&#x27;s add &lt;code&gt;tlhelp32&lt;&#x2F;code&gt; to the crate features of &lt;code&gt;winapi&lt;&#x2F;code&gt; and try it out:&lt;&#x2F;p&gt;
 518&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;
 519#[derive(Debug)]
 520pub struct Toolhelp {
 521    handle: winapi::um::winnt::HANDLE,
 522}
 523
 524impl Drop for Toolhelp {
 525    fn drop(&amp;amp;mut self) {
 526        unsafe { winapi::um::handleapi::CloseHandle(self.handle) };
 527    }
 528}
 529
 530pub fn enum_threads(pid: u32) -&amp;gt; io::Result&amp;lt;Vec&amp;lt;u32&amp;gt;&amp;gt; {
 531    const ENTRY_SIZE: u32 = mem::size_of::&amp;lt;winapi::um::tlhelp32::THREADENTRY32&amp;gt;() as u32;
 532
 533    &#x2F;&#x2F; size_of(dwSize + cntUsage + th32ThreadID + th32OwnerProcessID)
 534    const NEEDED_ENTRY_SIZE: u32 = 4 * mem::size_of::&amp;lt;DWORD&amp;gt;() as u32;
 535
 536    &#x2F;&#x2F; SAFETY: it is always safe to attempt to call this function.
 537    let handle = unsafe {
 538        winapi::um::tlhelp32::CreateToolhelp32Snapshot(winapi::um::tlhelp32::TH32CS_SNAPTHREAD, 0)
 539    };
 540    if handle == winapi::um::handleapi::INVALID_HANDLE_VALUE {
 541        return Err(io::Error::last_os_error());
 542    }
 543    let toolhelp = Toolhelp { handle };
 544
 545    let mut result = Vec::new();
 546    let mut entry = winapi::um::tlhelp32::THREADENTRY32 {
 547        dwSize: ENTRY_SIZE,
 548        cntUsage: 0,
 549        th32ThreadID: 0,
 550        th32OwnerProcessID: 0,
 551        tpBasePri: 0,
 552        tpDeltaPri: 0,
 553        dwFlags: 0,
 554    };
 555
 556    &#x2F;&#x2F; SAFETY: we have a valid handle, and point to memory we own with the right size.
 557    if unsafe { winapi::um::tlhelp32::Thread32First(toolhelp.handle, &amp;amp;mut entry) } != FALSE {
 558        loop {
 559            if entry.dwSize &amp;gt;= NEEDED_ENTRY_SIZE &amp;amp;&amp;amp; entry.th32OwnerProcessID == pid {
 560                result.push(entry.th32ThreadID);
 561            }
 562
 563            entry.dwSize = ENTRY_SIZE;
 564            &#x2F;&#x2F; SAFETY: we have a valid handle, and point to memory we own with the right size.
 565            if unsafe { winapi::um::tlhelp32::Thread32Next(toolhelp.handle, &amp;amp;mut entry) } == FALSE {
 566                break;
 567            }
 568        }
 569    }
 570
 571    Ok(result)
 572}
 573&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 574&lt;p&gt;Annoyingly, invalid handles returned by &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;tlhelp32&#x2F;nf-tlhelp32-createtoolhelp32snapshot&quot;&gt;&lt;code&gt;CreateToolhelp32Snapshot&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, are &lt;code&gt;INVALID_HANDLE_VALUE&lt;&#x2F;code&gt; (which is -1), not null. But that&#x27;s not a big deal, we simply can&#x27;t use &lt;code&gt;NonNull&lt;&#x2F;code&gt; here. The function ignores the process identifier when using &lt;code&gt;TH32CS_SNAPTHREAD&lt;&#x2F;code&gt;, used to include all threads, and we need to compare the process identifier ourselves.&lt;&#x2F;p&gt;
 575&lt;p&gt;In summary, we create a &amp;quot;toolhelp&amp;quot; (wrapped in a helper &lt;code&gt;struct&lt;&#x2F;code&gt; so that whatever happens, &lt;code&gt;Drop&lt;&#x2F;code&gt; will clean it up), initialize a thread enntry (with everything but the structure size to zero) and call &lt;code&gt;Thread32First&lt;&#x2F;code&gt; the first time, &lt;code&gt;Thread32Next&lt;&#x2F;code&gt; subsequent times. It seems to work all fine!&lt;&#x2F;p&gt;
 576&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;dbg!(process::enum_threads(pid));
 577&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 578&lt;pre&gt;&lt;code&gt;[src\main.rs:46] process::enum_threads(pid) = Ok(
 579    [
 580        10560,
 581    ],
 582)
 583&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 584&lt;p&gt;According to this, the Cheat Engine tutorial is only using one thread. Good to know. Much like processes, threads need to be opened before we can use them, with &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;processthreadsapi&#x2F;nf-processthreadsapi-openthread&quot;&gt;&lt;code&gt;OpenThread&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
 585&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub struct Thread {
 586    tid: u32,
 587    handle: NonNull&amp;lt;c_void&amp;gt;,
 588}
 589
 590impl Thread {
 591    pub fn open(tid: u32) -&amp;gt; io::Result&amp;lt;Self&amp;gt; {
 592        &#x2F;&#x2F; SAFETY: the call doesn&#x27;t have dangerous side-effects
 593        NonNull::new(unsafe {
 594            winapi::um::processthreadsapi::OpenThread(
 595                winapi::um::winnt::THREAD_SUSPEND_RESUME,
 596                FALSE,
 597                tid,
 598            )
 599        })
 600        .map(|handle| Self { tid, handle })
 601        .ok_or_else(io::Error::last_os_error)
 602    }
 603
 604    pub fn tid(&amp;amp;self) -&amp;gt; u32 {
 605        self.tid
 606    }
 607}
 608
 609impl Drop for Thread {
 610    fn drop(&amp;amp;mut self) {
 611        unsafe { winapi::um::handleapi::CloseHandle(self.handle.as_mut()) };
 612    }
 613}
 614&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 615&lt;p&gt;Just your usual RAII pattern. The thread is opened with permission to suspend and resume it. Let&#x27;s try to pause the handles with &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;processthreadsapi&#x2F;nf-processthreadsapi-suspendthread&quot;&gt;&lt;code&gt;SuspendThread&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; to make sure that this thread is actually the one we&#x27;re looking for:&lt;&#x2F;p&gt;
 616&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub fn suspend(&amp;amp;mut self) -&amp;gt; io::Result&amp;lt;usize&amp;gt; {
 617    &#x2F;&#x2F; SAFETY: the handle is valid.
 618    let ret = unsafe {
 619        winapi::um::processthreadsapi::SuspendThread(self.handle.as_ptr())
 620    };
 621    if ret == -1i32 as u32 {
 622        Err(io::Error::last_os_error())
 623    } else {
 624        Ok(ret as usize)
 625    }
 626}
 627
 628pub fn resume(&amp;amp;mut self) -&amp;gt; io::Result&amp;lt;usize&amp;gt; {
 629    &#x2F;&#x2F; SAFETY: the handle is valid.
 630    let ret = unsafe {
 631        winapi::um::processthreadsapi::ResumeThread(self.handle.as_ptr())
 632    };
 633    if ret == -1i32 as u32 {
 634        Err(io::Error::last_os_error())
 635    } else {
 636        Ok(ret as usize)
 637    }
 638}
 639&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 640&lt;p&gt;Both suspend and resume return the previous &amp;quot;suspend count&amp;quot;. It&#x27;s kind of like a barrier or semaphore where the thread only runs if the suspend count is zero. Trying it out:&lt;&#x2F;p&gt;
 641&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let mut threads = thread::enum_threads(pid)
 642    .unwrap()
 643    .into_iter()
 644    .map(Thread::open)
 645    .collect::&amp;lt;Result&amp;lt;Vec&amp;lt;_&amp;gt;, _&amp;gt;&amp;gt;()
 646    .unwrap();
 647
 648threads
 649    .iter_mut()
 650    .for_each(|thread| {
 651        println!(&amp;quot;Pausing thread {} for 10 seconds…&amp;quot;, thread.tid());
 652        thread.suspend().unwrap();
 653
 654        std::thread::sleep(std::time::Duration::from_secs(10));
 655
 656        println!(&amp;quot;Wake up, {}!&amp;quot;, thread.tid());
 657        thread.resume().unwrap();
 658    });
 659&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 660&lt;p&gt;If you run this code with the process ID of the Cheat Engine tutorial, you will see that the tutorial window freezes for ten seconds! Because the main and only thread is paused, it cannot process any window events, so it becomes unresponsive. It is now &amp;quot;safe&amp;quot; to mess around with the thread context.&lt;&#x2F;p&gt;
 661&lt;h2 id=&quot;setting-hardware-breakpoints&quot;&gt;Setting hardware breakpoints&lt;&#x2F;h2&gt;
 662&lt;p&gt;I&#x27;m definitely not the first person to wonder &lt;a href=&quot;https:&#x2F;&#x2F;social.msdn.microsoft.com&#x2F;Forums&#x2F;en-US&#x2F;0cb3360d-3747-42a7-bc0e-668c5d9ee1ee&#x2F;how-to-set-a-hardware-breakpoint&quot;&gt;How to set a hardware breakpoint?&lt;&#x2F;a&gt;. This is great, because it means I don&#x27;t need to ask that question myself. It appears we need to change the debug register &lt;em&gt;via the thread context&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
 663&lt;p&gt;One has to be careful to use the right context structure. Confusingly enough, &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;q&#x2F;17504174&#x2F;&quot;&gt;&lt;code&gt;WOW64_CONTEXT&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; is 32 bits, not 64. &lt;code&gt;CONTEXT&lt;&#x2F;code&gt; alone seems to be the right one:&lt;&#x2F;p&gt;
 664&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub fn get_context(&amp;amp;self) -&amp;gt; io::Result&amp;lt;winapi::um::winnt::CONTEXT&amp;gt; {
 665    let context = MaybeUninit::&amp;lt;winapi::um::winnt::CONTEXT&amp;gt;::zeroed();
 666    &#x2F;&#x2F; SAFETY: it&#x27;s a C struct, and all-zero is a valid bit-pattern for the type.
 667    let mut context = unsafe { context.assume_init() };
 668    context.ContextFlags = winapi::um::winnt::CONTEXT_ALL;
 669
 670    &#x2F;&#x2F; SAFETY: the handle is valid and structure points to valid memory.
 671    if unsafe {
 672        winapi::um::processthreadsapi::GetThreadContext(self.handle.as_ptr(), &amp;amp;mut context)
 673    } == FALSE
 674    {
 675        Err(io::Error::last_os_error())
 676    } else {
 677        Ok(context)
 678    }
 679}
 680&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 681&lt;p&gt;Trying it out:&lt;&#x2F;p&gt;
 682&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;thread.suspend().unwrap();
 683
 684let context = thread.get_context().unwrap();
 685println!(&amp;quot;Dr0: {:016x}&amp;quot;, context.Dr0);
 686println!(&amp;quot;Dr7: {:016x}&amp;quot;, context.Dr7);
 687println!(&amp;quot;Dr6: {:016x}&amp;quot;, context.Dr6);
 688println!(&amp;quot;Rax: {:016x}&amp;quot;, context.Rax);
 689println!(&amp;quot;Rbx: {:016x}&amp;quot;, context.Rbx);
 690println!(&amp;quot;Rcx: {:016x}&amp;quot;, context.Rcx);
 691println!(&amp;quot;Rip: {:016x}&amp;quot;, context.Rip);
 692&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 693&lt;pre&gt;&lt;code&gt;Dr0: 0000000000000000
 694Dr7: 0000000000000000
 695Dr6: 0000000000000000
 696Rax: 0000000000001446
 697Rbx: 0000000000000000
 698Rcx: 0000000000000000
 699Rip: 00007ffda4259904
 700&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 701&lt;p&gt;Looks about right! Hm, I wonder what happens if I use Cheat Engine to add the watchpoint on the memory location we care about?&lt;&#x2F;p&gt;
 702&lt;pre&gt;&lt;code&gt;Dr0: 000000000157e650
 703Dr7: 00000000000d0001
 704&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 705&lt;p&gt;Look at that! The debug registers changed! DR0 contains the location we want to watch for writes, and the debug control register DR7 changed. Cheat Engine sets the same values on all threads (for some reason I now see more than one thread printed for the tutorial, not sure what&#x27;s up with that; maybe the single-thread is the weird one out).&lt;&#x2F;p&gt;
 706&lt;p&gt;Hmm, what happens if I watch for access instead of write?&lt;&#x2F;p&gt;
 707&lt;pre&gt;&lt;code&gt;Dr0: 000000000157e650
 708Dr7: 00000000000f0001
 709&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 710&lt;p&gt;What if I set both?&lt;&#x2F;p&gt;
 711&lt;pre&gt;&lt;code&gt;Dr0: 000000000157e650
 712Dr7: 0000000000fd0005
 713&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 714&lt;p&gt;Most intriguing! This was done by telling Cheat Engine to find &amp;quot;what writes&amp;quot; to the address, then &amp;quot;what accesses&amp;quot; the address. I wonder if the order matters?&lt;&#x2F;p&gt;
 715&lt;pre&gt;&lt;code&gt;Dr0: 000000000157e650
 716Dr7: 0000000000df0005
 717&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 718&lt;p&gt;&amp;quot;What accesses&amp;quot; and then &amp;quot;what writes&amp;quot; does change it. Very well! We&#x27;re only concerned in a single breakpoint, so we won&#x27;t worry about this, but it&#x27;s good to know that we can inspect what Cheat Engine is doing. It&#x27;s also interesting to see how Cheat Engine is using hardware breakpoints and not software breakpoints.&lt;&#x2F;p&gt;
 719&lt;p&gt;For simplicity, our code is going to assume that we&#x27;re the only ones messing around with the debug registers, and that there will only be a single debug register in use. Make sure to add &lt;code&gt;THREAD_SET_CONTEXT&lt;&#x2F;code&gt; to the permissions when opening the thread handle:&lt;&#x2F;p&gt;
 720&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub fn set_context(&amp;amp;self, context: &amp;amp;winapi::um::winnt::CONTEXT) -&amp;gt; io::Result&amp;lt;()&amp;gt; {
 721    &#x2F;&#x2F; SAFETY: the handle is valid and structure points to valid memory.
 722    if unsafe {
 723        winapi::um::processthreadsapi::SetThreadContext(self.handle.as_ptr(), context)
 724    } == FALSE
 725    {
 726        Err(io::Error::last_os_error())
 727    } else {
 728        Ok(())
 729    }
 730}
 731
 732pub fn watch_memory_write(&amp;amp;self, addr: usize) -&amp;gt; io::Result&amp;lt;()&amp;gt; {
 733    let mut context = self.get_context()?;
 734    context.Dr0 = addr as u64;
 735    context.Dr7 = 0x00000000000d0001;
 736    self.set_context(&amp;amp;context)?;
 737    todo!()
 738}
 739&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 740&lt;p&gt;If we do this (and temporarily get rid of the &lt;code&gt;todo!()&lt;&#x2F;code&gt;), trying to change the value in the Cheat Engine tutorial will greet us with a warm message:&lt;&#x2F;p&gt;
 741&lt;blockquote&gt;
 742&lt;p&gt;&lt;strong&gt;Tutorial-x86_64&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
 743&lt;p&gt;External exception 80000004.&lt;&#x2F;p&gt;
 744&lt;p&gt;Press OK to ignore and risk data corruption.&lt;br &#x2F;&gt;
 745Press Abort to kill the program.&lt;&#x2F;p&gt;
 746&lt;p&gt;&lt;kbd&gt;OK&lt;&#x2F;kbd&gt; &lt;kbd&gt;Abort&lt;&#x2F;kbd&gt;&lt;&#x2F;p&gt;
 747&lt;&#x2F;blockquote&gt;
 748&lt;p&gt;There is no debugger attached yet that could possibly handle this exception, so the exception just propagates. Let&#x27;s fix that.&lt;&#x2F;p&gt;
 749&lt;h2 id=&quot;handling-debug-events&quot;&gt;Handling debug events&lt;&#x2F;h2&gt;
 750&lt;p&gt;Now that we&#x27;ve succeeded on setting breakpoints, we can actually follow the steps described in &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;debug&#x2F;creating-a-basic-debugger&quot;&gt;Creating a Basic Debugger&lt;&#x2F;a&gt;. It starts by saying that we should use &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;debugapi&#x2F;nf-debugapi-debugactiveprocess&quot;&gt;&lt;code&gt;DebugActiveProcess&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; to attach our processor, the debugger, to the process we want to debug, the debuggee. This function lives under the &lt;code&gt;debugapi&lt;&#x2F;code&gt; header, so add it to &lt;code&gt;winapi&lt;&#x2F;code&gt; features:&lt;&#x2F;p&gt;
 751&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub struct DebugToken {
 752    pid: u32,
 753}
 754
 755pub fn debug(pid: u32) -&amp;gt; io::Result&amp;lt;DebugToken&amp;gt; {
 756    if unsafe { winapi::um::debugapi::DebugActiveProcess(pid) } == FALSE {
 757        return Err(io::Error::last_os_error());
 758    };
 759    let token = DebugToken { pid };
 760    if unsafe { winapi::um::winbase::DebugSetProcessKillOnExit(FALSE) } == FALSE {
 761        return Err(io::Error::last_os_error());
 762    };
 763    Ok(token)
 764}
 765
 766impl Drop for DebugToken {
 767    fn drop(&amp;amp;mut self) {
 768        unsafe { winapi::um::debugapi::DebugActiveProcessStop(self.pid) };
 769    }
 770}
 771&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 772&lt;p&gt;Once again, we create a wrapper &lt;code&gt;struct&lt;&#x2F;code&gt; with &lt;code&gt;Drop&lt;&#x2F;code&gt; to stop debugging the process once the token is dropped. The call to &lt;code&gt;DebugSetProcessKillOnExit&lt;&#x2F;code&gt; in our &lt;code&gt;debug&lt;&#x2F;code&gt; method ensures that, if our process (the debugger) dies, the process we&#x27;re debugging (the debuggee) stays alive. We don&#x27;t want to be restarting the entire Cheat Engine tutorial every time our Rust code crashes!&lt;&#x2F;p&gt;
 773&lt;p&gt;With the debugger attached, we can wait for debug events. We will put this method inside of &lt;code&gt;impl DebugToken&lt;&#x2F;code&gt;, so that the only way you can call it is if you successfully attached to another process:&lt;&#x2F;p&gt;
 774&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;impl DebugToken {
 775    pub fn wait_event(
 776        &amp;amp;self,
 777        timeout: Option&amp;lt;Duration&amp;gt;,
 778    ) -&amp;gt; io::Result&amp;lt;winapi::um::minwinbase::DEBUG_EVENT&amp;gt; {
 779        let mut result = MaybeUninit::uninit();
 780        let timeout = timeout
 781            .map(|d| d.as_millis().try_into().ok())
 782            .flatten()
 783            .unwrap_or(winapi::um::winbase::INFINITE);
 784
 785        &#x2F;&#x2F; SAFETY: can only wait for events with a token, so the debugger is active.
 786        if unsafe { winapi::um::debugapi::WaitForDebugEvent(result.as_mut_ptr(), timeout) } == FALSE
 787        {
 788            Err(io::Error::last_os_error())
 789        } else {
 790            &#x2F;&#x2F; SAFETY: the call returned non-zero, so the structure is initialized.
 791            Ok(unsafe { result.assume_init() })
 792        }
 793    }
 794}
 795&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 796&lt;p&gt;&lt;code&gt;WaitForDebugEvent&lt;&#x2F;code&gt; wants a timeout in milliseconds, so our function lets the user pass the more Rusty &lt;code&gt;Duration&lt;&#x2F;code&gt; type. &lt;code&gt;None&lt;&#x2F;code&gt; will indicate &amp;quot;there is no timeout&amp;quot;, i.e., it&#x27;s infinite. If the duration is too large to fit in the &lt;code&gt;u32&lt;&#x2F;code&gt; (&lt;code&gt;try_into&lt;&#x2F;code&gt; fails), it will also be infinite.&lt;&#x2F;p&gt;
 797&lt;p&gt;If we attach the debugger, set the hardware watchpoint, and modify the memory location from the tutorial, an event with &lt;code&gt;dwDebugEventCode = 3&lt;&#x2F;code&gt; will be returned! Now, back to the page with the &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;debug&#x2F;debugging-events&quot;&gt;Debugging Events&lt;&#x2F;a&gt;… Gah! It only has the name of the constants, not the values. Well, good thing &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;&quot;&gt;docs.rs&lt;&#x2F;a&gt; has a source view! We can just check the values in the &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;winapi&#x2F;0.3.9&#x2F;src&#x2F;winapi&#x2F;um&#x2F;minwinbase.rs.html#203-211&quot;&gt;source code for &lt;code&gt;winapi&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
 798&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub const EXCEPTION_DEBUG_EVENT: DWORD = 1;
 799pub const CREATE_THREAD_DEBUG_EVENT: DWORD = 2;
 800pub const CREATE_PROCESS_DEBUG_EVENT: DWORD = 3;
 801pub const EXIT_THREAD_DEBUG_EVENT: DWORD = 4;
 802pub const EXIT_PROCESS_DEBUG_EVENT: DWORD = 5;
 803pub const LOAD_DLL_DEBUG_EVENT: DWORD = 6;
 804pub const UNLOAD_DLL_DEBUG_EVENT: DWORD = 7;
 805pub const OUTPUT_DEBUG_STRING_EVENT: DWORD = 8;
 806pub const RIP_EVENT: DWORD = 9;
 807&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 808&lt;p&gt;So, we&#x27;ve got a &lt;code&gt;CREATE_PROCESS_DEBUG_EVENT&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
 809&lt;blockquote&gt;
 810&lt;p&gt;Generated whenever a new process is created in a process being debugged or whenever the debugger begins debugging an already active process. The system generates this debugging event before the process begins to execute in user mode and before the system generates any other debugging events for the new process.&lt;&#x2F;p&gt;
 811&lt;&#x2F;blockquote&gt;
 812&lt;p&gt;It makes sense that this is our first event. By the way, if you were trying this out with a &lt;code&gt;sleep&lt;&#x2F;code&gt; lying around in your code, you may have noticed that the window froze until the debugger terminated. That&#x27;s because:&lt;&#x2F;p&gt;
 813&lt;blockquote&gt;
 814&lt;p&gt;When the system notifies the debugger of a debugging event, it also suspends all threads in the affected process. The threads do not resume execution until the debugger continues the debugging event by using &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;debugapi&#x2F;nf-debugapi-continuedebugevent&quot;&gt;&lt;code&gt;ContinueDebugEvent&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
 815&lt;&#x2F;blockquote&gt;
 816&lt;p&gt;Let&#x27;s call &lt;code&gt;ContinueDebugMethod&lt;&#x2F;code&gt; but also wait on more than one event and see what happens:&lt;&#x2F;p&gt;
 817&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;for _ in 0..10 {
 818    let event = debugger.wait_event(None).unwrap();
 819    println!(&amp;quot;Got {}&amp;quot;, event.dwDebugEventCode);
 820    debugger.cont(event, true).unwrap();
 821}
 822&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 823&lt;pre&gt;&lt;code&gt;Got 3
 824Got 6
 825Got 6
 826Got 6
 827Got 6
 828Got 6
 829Got 6
 830Got 6
 831Got 6
 832Got 6
 833&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 834&lt;p&gt;That&#x27;s a lot of &lt;code&gt;LOAD_DLL_DEBUG_EVENT&lt;&#x2F;code&gt;. Pumping it up to one hundred and also showing the index we get the following:&lt;&#x2F;p&gt;
 835&lt;pre&gt;&lt;code&gt;0. Got 3
 8361. Got 6
 837...
 83840. Got 6
 83941. Got 2
 84042. Got 1
 84143. Got 4
 842&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 843&lt;p&gt;In order, we got:&lt;&#x2F;p&gt;
 844&lt;ul&gt;
 845&lt;li&gt;One &lt;code&gt;CREATE_PROCESS_DEBUG_EVENT&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
 846&lt;li&gt;Forty &lt;code&gt;LOAD_DLL_DEBUG_EVENT&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
 847&lt;li&gt;One &lt;code&gt;CREATE_THREAD_DEBUG_EVENT&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
 848&lt;li&gt;One &lt;code&gt;EXCEPTION_DEBUG_EVENT&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
 849&lt;li&gt;One &lt;code&gt;EXIT_THREAD_DEBUG_EVENT&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
 850&lt;&#x2F;ul&gt;
 851&lt;p&gt;And, if after all this, you change the value in the Cheat Engine tutorial (thus triggering our watch point), we get &lt;code&gt;EXCEPTION_DEBUG_EVENT&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
 852&lt;blockquote&gt;
 853&lt;p&gt;Generated whenever an exception occurs in the process being debugged. Possible exceptions include attempting to access inaccessible memory, executing breakpoint instructions, attempting to divide by zero, or any other exception noted in Structured Exception Handling.&lt;&#x2F;p&gt;
 854&lt;&#x2F;blockquote&gt;
 855&lt;p&gt;If we print out all the fields in the &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;minwinbase&#x2F;ns-minwinbase-exception_debug_info&quot;&gt;&lt;code&gt;EXCEPTION_DEBUG_INFO&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; structure:&lt;&#x2F;p&gt;
 856&lt;pre&gt;&lt;code&gt;Watching writes to 10e3a0 for 10s
 857First chance: 1
 858ExceptionCode: 2147483652
 859ExceptionFlags: 0
 860ExceptionRecord: 0x0
 861ExceptionAddress: 0x10002c5ba
 862NumberParameters: 0
 863ExceptionInformation: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
 864&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 865&lt;p&gt;The &lt;code&gt;ExceptionCode&lt;&#x2F;code&gt;, which is &lt;code&gt;0x80000004&lt;&#x2F;code&gt;, corresponds with &lt;code&gt;EXCEPTION_SINGLE_STEP&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
 866&lt;blockquote&gt;
 867&lt;p&gt;A trace trap or other single-instruction mechanism signaled that one instruction has been executed.&lt;&#x2F;p&gt;
 868&lt;&#x2F;blockquote&gt;
 869&lt;p&gt;The &lt;code&gt;ExceptionAddress&lt;&#x2F;code&gt; is supposed to be &amp;quot;the address where the exception occurred&amp;quot;. Very well! I have already completed this step of the tutorial, and I know the instruction is &lt;code&gt;mov [rax],edx&lt;&#x2F;code&gt; (or, as Cheat Engine shows, the bytes &lt;code&gt;89 10&lt;&#x2F;code&gt; in hexadecimal). The opcode for the &lt;code&gt;nop&lt;&#x2F;code&gt; instruction is &lt;code&gt;90&lt;&#x2F;code&gt; in hexadecimal, so if we replace two bytes at this address, we should be able to complete the tutorial.&lt;&#x2F;p&gt;
 870&lt;p&gt;Note that we also need to flush the instruction cache, as noted in the Windows documentation:&lt;&#x2F;p&gt;
 871&lt;blockquote&gt;
 872&lt;p&gt;Debuggers frequently read the memory of the process being debugged and write the memory that contains instructions to the instruction cache. After the instructions are written, the debugger calls the &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;processthreadsapi&#x2F;nf-processthreadsapi-flushinstructioncache&quot;&gt;&lt;code&gt;FlushInstructionCache&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; function to execute the cached instructions.&lt;&#x2F;p&gt;
 873&lt;&#x2F;blockquote&gt;
 874&lt;p&gt;So we add a new method to &lt;code&gt;impl Process&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
 875&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&#x2F;&#x2F;&#x2F; Flushes the instruction cache.
 876&#x2F;&#x2F;&#x2F;
 877&#x2F;&#x2F;&#x2F; Should be called when writing to memory regions that contain code.
 878pub fn flush_instruction_cache(&amp;amp;self) -&amp;gt; io::Result&amp;lt;()&amp;gt; {
 879    &#x2F;&#x2F; SAFETY: the call doesn&#x27;t have dangerous side-effects.
 880    if unsafe {
 881        winapi::um::processthreadsapi::FlushInstructionCache(
 882            self.handle.as_ptr(),
 883            ptr::null(),
 884            0,
 885        )
 886    } == FALSE
 887    {
 888        Err(io::Error::last_os_error())
 889    } else {
 890        Ok(())
 891    }
 892}
 893&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 894&lt;p&gt;And write some quick and dirty code to get this done:&lt;&#x2F;p&gt;
 895&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let addr = ...;
 896println!(&amp;quot;Watching writes to {:x} for 10s&amp;quot;, addr);
 897threads.iter_mut().for_each(|thread| {
 898    thread.watch_memory_write(addr).unwrap();
 899});
 900loop {
 901    let event = debugger.wait_event(None).unwrap();
 902    if event.dwDebugEventCode == 1 {
 903        let exc = unsafe { event.u.Exception() };
 904        if exc.ExceptionRecord.ExceptionCode == 2147483652 {
 905            let addr = exc.ExceptionRecord.ExceptionAddress as usize;
 906            match process.write_memory(addr, &amp;amp;[0x90, 0x90]) {
 907                Ok(_) =&amp;gt; eprintln!(&amp;quot;Patched [{:x}] with NOP&amp;quot;, addr),
 908                Err(e) =&amp;gt; eprintln!(&amp;quot;Failed to patch [{:x}] with NOP: {}&amp;quot;, addr, e),
 909            };
 910            process.flush_instruction_cache().unwrap();
 911            debugger.cont(event, true).unwrap();
 912            break;
 913        }
 914    }
 915    debugger.cont(event, true).unwrap();
 916}
 917&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 918&lt;p&gt;Although it seems to work:&lt;&#x2F;p&gt;
 919&lt;pre&gt;&lt;code&gt;Watching writes to 15103f0 for 10s
 920Patched [10002c5ba] with NOP
 921&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 922&lt;p&gt;It really doesn&#x27;t:&lt;&#x2F;p&gt;
 923&lt;blockquote&gt;
 924&lt;p&gt;&lt;strong&gt;Tutorial-x86_64&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
 925&lt;p&gt;Access violation.&lt;&#x2F;p&gt;
 926&lt;p&gt;Press OK to ignore and risk data corruption.&lt;br &#x2F;&gt;
 927Press Abort to kill the program.&lt;&#x2F;p&gt;
 928&lt;p&gt;&lt;kbd&gt;OK&lt;&#x2F;kbd&gt; &lt;kbd&gt;Abort&lt;&#x2F;kbd&gt;&lt;&#x2F;p&gt;
 929&lt;&#x2F;blockquote&gt;
 930&lt;p&gt;Did we write memory somewhere we shouldn&#x27;t? The documentation does mention &amp;quot;segment-relative&amp;quot; and &amp;quot;linear virtual addresses&amp;quot;:&lt;&#x2F;p&gt;
 931&lt;blockquote&gt;
 932&lt;p&gt;&lt;code&gt;GetThreadSelectorEntry&lt;&#x2F;code&gt; returns the descriptor table entry for a specified selector and thread. Debuggers use the descriptor table entry to convert a segment-relative address to a linear virtual address. The &lt;code&gt;ReadProcessMemory&lt;&#x2F;code&gt; and &lt;code&gt;WriteProcessMemory&lt;&#x2F;code&gt; functions require linear virtual addresses.&lt;&#x2F;p&gt;
 933&lt;&#x2F;blockquote&gt;
 934&lt;p&gt;But nope! This isn&#x27;t the problem. The problem is that the &lt;code&gt;ExceptionRecord.ExceptionAddress&lt;&#x2F;code&gt; is &lt;em&gt;after&lt;&#x2F;em&gt; the execution happened, so it&#x27;s already 2 bytes beyond where it should be. We were accidentally writing out the first half of the next instruction, which, yeah, could not end good.&lt;&#x2F;p&gt;
 935&lt;p&gt;So does it work if I do this instead?:&lt;&#x2F;p&gt;
 936&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;process.write_memory(addr - 2, &amp;amp;[0x90, 0x90])
 937&#x2F;&#x2F;                        ^^^ new
 938&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 939&lt;p&gt;This totally does work. Step 5: complete 🎉&lt;&#x2F;p&gt;
 940&lt;h2 id=&quot;properly-patching-instructions&quot;&gt;Properly patching instructions&lt;&#x2F;h2&gt;
 941&lt;p&gt;You may not be satisfied at all with our solution. Not only are we hardcoding some magic constants to set hardware watchpoints, we&#x27;re also relying on knowledge specific to the Cheat Engine tutorial (insofar that we&#x27;re replacing two bytes worth of instruction with NOPs).&lt;&#x2F;p&gt;
 942&lt;p&gt;Properly supporting more than one hardware breakpoint, along with supporting different types of breakpoints, is definitely doable. The meaning of the bits for the debug registers is well defined, and you can definitely study that to come up with &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;mmorearty&#x2F;hardware-breakpoints&quot;&gt;something more sophisticated&lt;&#x2F;a&gt; and support multiple different breakpoints. But for now, that&#x27;s out of the scope of this series. The tutorial only wants us to use an on-write watchpoint, and our solution is fine and portable for that use case.&lt;&#x2F;p&gt;
 943&lt;p&gt;However, relying on the size of the instructions is pretty bad. The instructions x86 executes are of variable length, so we can&#x27;t possibly just look back until we find the previous instruction, or even naively determine its length. A lot of unrelated sequences of bytes are very likely instructions themselves. We need a disassembler. No, we&#x27;re not writing our own&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#4&quot;&gt;4&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;.&lt;&#x2F;p&gt;
 944&lt;p&gt;Searching on &lt;a href=&quot;https:&#x2F;&#x2F;crates.io&quot;&gt;crates.io&lt;&#x2F;a&gt; for &amp;quot;disassembler&amp;quot; yields a few results, and the first one I&#x27;ve found is &lt;a href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;iced-x86&quot;&gt;iced-x86&lt;&#x2F;a&gt;. I like the name, it has a decent amount of GitHub stars, and it was last updated less than a month ago. I don&#x27;t know about you, but I think we&#x27;ve just hit a jackpot!&lt;&#x2F;p&gt;
 945&lt;p&gt;It&#x27;s quite heavy though, so I will add it behind a feature gate, and users that want it may opt into it:&lt;&#x2F;p&gt;
 946&lt;pre&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;[features]
 947patch-nops = [&amp;quot;iced-x86&amp;quot;]
 948
 949[dependencies]
 950iced-x86 = { version = &amp;quot;1.10.3&amp;quot;, optional = true }
 951&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 952&lt;p&gt;You can make use of it with &lt;code&gt;cargo run --features=patch-nops&lt;&#x2F;code&gt;. I don&#x27;t want to turn this blog post into a tutorial for &lt;code&gt;iced-x86&lt;&#x2F;code&gt;, but in essence, we need to make use of its &lt;code&gt;Decoder&lt;&#x2F;code&gt;. Here&#x27;s the plan:&lt;&#x2F;p&gt;
 953&lt;ol&gt;
 954&lt;li&gt;Find the memory region corresponding to the address we want to patch.&lt;&#x2F;li&gt;
 955&lt;li&gt;Read the entire region.&lt;&#x2F;li&gt;
 956&lt;li&gt;Decode the read bytes until the instruction pointer reaches our address.&lt;&#x2F;li&gt;
 957&lt;li&gt;Because we just parsed the previous instruction, we know its length, and can be replaced with NOPs.&lt;&#x2F;li&gt;
 958&lt;&#x2F;ol&gt;
 959&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;#[cfg(feature = &amp;quot;patch-nops&amp;quot;)]
 960pub fn nop_last_instruction(&amp;amp;self, addr: usize) -&amp;gt; io::Result&amp;lt;()&amp;gt; {
 961    use iced_x86::{Decoder, DecoderOptions, Formatter, Instruction, NasmFormatter};
 962
 963    let region = self
 964        .memory_regions()
 965        .into_iter()
 966        .find(|region| {
 967            let base = region.BaseAddress as usize;
 968            base &amp;lt;= addr &amp;amp;&amp;amp; addr &amp;lt; base + region.RegionSize
 969        })
 970        .ok_or_else(|| io::Error::new(io::ErrorKind::Other, &amp;quot;no matching region found&amp;quot;))?;
 971
 972    let bytes = self.read_memory(region.BaseAddress as usize, region.RegionSize)?;
 973
 974    let mut decoder = Decoder::new(64, &amp;amp;bytes, DecoderOptions::NONE);
 975    decoder.set_ip(region.BaseAddress as _);
 976
 977    let mut instruction = Instruction::default();
 978    while decoder.can_decode() {
 979        decoder.decode_out(&amp;amp;mut instruction);
 980        if instruction.next_ip() as usize == addr {
 981            return self
 982                .write_memory(instruction.ip() as usize, &amp;amp;vec![0x90; instruction.len()])
 983                .map(drop);
 984        }
 985    }
 986
 987    Err(io::Error::new(
 988        io::ErrorKind::Other,
 989        &amp;quot;no matching instruction found&amp;quot;,
 990    ))
 991}
 992&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
 993&lt;p&gt;Pretty straightforward! We can set the &amp;quot;instruction pointer&amp;quot; of the decoder so that it matches with the address we&#x27;re reading from. The &lt;code&gt;next_ip&lt;&#x2F;code&gt; method comes in really handy. Overall, it&#x27;s a bit inefficient, because we could reuse the regions retrieved previously, but other than that, there is not much room for improvement.&lt;&#x2F;p&gt;
 994&lt;p&gt;With this, we are no longer hardcoding the instruction size or guessing which instruction is doing what. You may wonder, what if the region does not start with valid executable code? It could be possible that the instructions are in some memory region with garbage except for a very specific location with real code. I don&#x27;t know how Cheat Engine handles this, but I think it&#x27;s reasonable to assume that the region starts with valid code.&lt;&#x2F;p&gt;
 995&lt;p&gt;As far as I can tell (after having asked a bit around), the encoding is usually self synchronizing (similar to UTF-8), so eventually we should end up with correct instructions. But someone can still intentionally write real code between garbage data which we would then disassemble incorrectly. This is a problem on all variable-length ISAs. Half a solution is to &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;q&#x2F;3983735&#x2F;&quot;&gt;start at the entry point&lt;&#x2F;a&gt;, decode all instructions, and follow the jumps. The other half would be correctly identifying jumps created just to trip a disassembler up, and jumps pointing to dynamically-calculated addresses!&lt;&#x2F;p&gt;
 996&lt;h2 id=&quot;finale&quot;&gt;Finale&lt;&#x2F;h2&gt;
 997&lt;p&gt;That was quite a deep dive! We have learnt about the existence of the various breakpoint types (software, hardware, and even behaviour, such as watchpoints), how to debug a separate process, and how to correctly update the code other process is running on-the-fly. The &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lonami&#x2F;memo&quot;&gt;code for this post&lt;&#x2F;a&gt; is available over at my GitHub. You can run &lt;code&gt;git checkout step5&lt;&#x2F;code&gt; after cloning the repository to get the right version of the code.&lt;&#x2F;p&gt;
 998&lt;p&gt;Although we&#x27;ve only talked about &lt;em&gt;setting&lt;&#x2F;em&gt; breakpoints, there are of course &lt;a href=&quot;https:&#x2F;&#x2F;reverseengineering.stackexchange.com&#x2F;a&#x2F;16547&quot;&gt;ways of detecting them&lt;&#x2F;a&gt;. There&#x27;s &lt;a href=&quot;https:&#x2F;&#x2F;www.codeproject.com&#x2F;Articles&#x2F;30815&#x2F;An-Anti-Reverse-Engineering-Guide&quot;&gt;entire guides about it&lt;&#x2F;a&gt;. Again, we currently hardcode the fact we want to add a single watchpoint using the first debug register. A proper solution here would be to actually calculate the needs that need to be set, as well as keeping track of how many breakpoints have been added so far.&lt;&#x2F;p&gt;
 999&lt;p&gt;Hardware breakpoints are also limited, since they&#x27;re simply a bunch of registers, and our machine does not have infinite registers. How are other debuggers like &lt;code&gt;gdb&lt;&#x2F;code&gt; able to create a seemingly unlimited amount of breakpoints? Well, the GDB wiki actually has a page on &lt;a href=&quot;https:&#x2F;&#x2F;sourceware.org&#x2F;gdb&#x2F;wiki&#x2F;Internals%20Watchpoints&quot;&gt;Internals Watchpoints&lt;&#x2F;a&gt;, and it&#x27;s really interesting! &lt;code&gt;gdb&lt;&#x2F;code&gt; essentially single-steps through the entire program and tests the expressions after every instruction:&lt;&#x2F;p&gt;
1000&lt;blockquote&gt;
1001&lt;p&gt;Software watchpoints are very slow, since GDB needs to single-step the program being debugged and test the value of the watched expression(s) after each instruction.&lt;&#x2F;p&gt;
1002&lt;&#x2F;blockquote&gt;
1003&lt;p&gt;However, that&#x27;s not the only way. One could &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;7805842&#x2F;&quot;&gt;change the protection level&lt;&#x2F;a&gt; of the region of interest (for example, remove the write permission), and when the program tries to write there, it will fail! In any case, the GDB wiki is actually a pretty nice resource. It also has a section on &lt;a href=&quot;https:&#x2F;&#x2F;sourceware.org&#x2F;gdb&#x2F;wiki&#x2F;Internals&#x2F;Breakpoint%20Handling&quot;&gt;Breakpoint Handling&lt;&#x2F;a&gt;, which contains some additional insight.&lt;&#x2F;p&gt;
1004&lt;p&gt;With regards to code improvements, &lt;code&gt;DebugToken::wait_event&lt;&#x2F;code&gt; could definitely be both nicer and safer to use, with a custom &lt;code&gt;enum&lt;&#x2F;code&gt;, so the user does not need to rely on magic constants or having to resort to &lt;code&gt;unsafe&lt;&#x2F;code&gt; access to get the right &lt;code&gt;union&lt;&#x2F;code&gt; variant.&lt;&#x2F;p&gt;
1005&lt;p&gt;In the &lt;a href=&quot;&#x2F;blog&#x2F;woce-6&quot;&gt;next post&lt;&#x2F;a&gt;, we&#x27;ll tackle the sixth step of the tutorial: Pointers. It reuses the debugging techniques presented here to backtrack where the pointer for our desired value is coming from, so here we will need to actually &lt;em&gt;understand&lt;&#x2F;em&gt; what the instructions are doing, not just patching them out!&lt;&#x2F;p&gt;
1006&lt;h3 id=&quot;footnotes&quot;&gt;Footnotes&lt;&#x2F;h3&gt;
1007&lt;div class=&quot;footnote-definition&quot; id=&quot;1&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;&#x2F;sup&gt;
1008&lt;p&gt;I&#x27;m not super happy about the design of it all, but we won&#x27;t actually need anything beyond scanning for integers for the rest of the steps so it doesn&#x27;t really matter.&lt;&#x2F;p&gt;
1009&lt;&#x2F;div&gt;
1010&lt;div class=&quot;footnote-definition&quot; id=&quot;2&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;2&lt;&#x2F;sup&gt;
1011&lt;p&gt;There seems to be a way to pause the entire process in one go, with the &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;4062698&#x2F;&quot;&gt;undocumented &lt;code&gt;NtSuspendProcess&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; function!&lt;&#x2F;p&gt;
1012&lt;&#x2F;div&gt;
1013&lt;div class=&quot;footnote-definition&quot; id=&quot;3&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;3&lt;&#x2F;sup&gt;
1014&lt;p&gt;It really is called that. The naming went from &amp;quot;IP&amp;quot; (instruction pointer, 16 bits), to &amp;quot;EIP&amp;quot; (extended instruction pointer, 32 bits) and currently &amp;quot;RIP&amp;quot; (64 bits). The naming convention for upgraded registers is the same (RAX, RBX, RCX, and so on). The &lt;a href=&quot;https:&#x2F;&#x2F;wiki.osdev.org&#x2F;CPU_Registers_x86_64&quot;&gt;OS Dev wiki&lt;&#x2F;a&gt; is a great resource for this kind of stuff.&lt;&#x2F;p&gt;
1015&lt;&#x2F;div&gt;
1016&lt;div class=&quot;footnote-definition&quot; id=&quot;4&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;4&lt;&#x2F;sup&gt;
1017&lt;p&gt;Well, we don&#x27;t need an entire disassembler. Knowing the length of each instruction is enough, but that on its own is also a lot of work.&lt;&#x2F;p&gt;
1018&lt;&#x2F;div&gt;
1019</content>
1020	</entry>
1021	<entry xml:lang="en">
1022		<title>Writing our own Cheat Engine: Floating points</title>
1023		<published>2021-02-28T00:00:00+00:00</published>
1024		<updated>2021-02-28T00:00:00+00:00</updated>
1025		<link href="https://lonami.dev/blog/woce-4/" type="text/html"/>
1026		<id>https://lonami.dev/blog/woce-4/</id>
1027		<content type="html">&lt;p&gt;This is part 4 on the &lt;em&gt;Writing our own Cheat Engine&lt;&#x2F;em&gt; series:&lt;&#x2F;p&gt;
1028&lt;ul&gt;
1029&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-1&quot;&gt;Part 1: Introduction&lt;&#x2F;a&gt; (start here if you&#x27;re new to the series!)&lt;&#x2F;li&gt;
1030&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-2&quot;&gt;Part 2: Exact Value scanning&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
1031&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-3&quot;&gt;Part 3: Unknown initial value&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
1032&lt;li&gt;Part 4: Floating points&lt;&#x2F;li&gt;
1033&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-5&quot;&gt;Part 5: Code finder&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
1034&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-6&quot;&gt;Part 6: Pointers&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
1035&lt;&#x2F;ul&gt;
1036&lt;p&gt;In part 3 we did a fair amount of plumbing in order to support scan modes beyond the trivial &amp;quot;exact value scan&amp;quot;. As a result, we have abstracted away the &lt;code&gt;Scan&lt;&#x2F;code&gt;, &lt;code&gt;CandidateLocations&lt;&#x2F;code&gt; and &lt;code&gt;Value&lt;&#x2F;code&gt; types as a separate &lt;code&gt;enum&lt;&#x2F;code&gt; each. Scanning for changed memory regions in an opened process can now be achieved with three lines of code:&lt;&#x2F;p&gt;
1037&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let regions = process.memory_regions();
1038let first_scan = process.scan_regions(&amp;amp;regions, Scan::InRange(0, 500));
1039let second_scan = process.rescan_regions(&amp;amp;first_scan, Scan::DecreasedBy(7));
1040&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1041&lt;p&gt;How&#x27;s that for programmability? No need to fire up Cheat Engine&#x27;s GUI anymore!&lt;&#x2F;p&gt;
1042&lt;p&gt;The &lt;code&gt;first_scan&lt;&#x2F;code&gt; in the example above remembers all the found &lt;code&gt;Value&lt;&#x2F;code&gt; within the range specified by &lt;code&gt;Scan&lt;&#x2F;code&gt;. Up until now, we have only worked with &lt;code&gt;i32&lt;&#x2F;code&gt;, so that&#x27;s the type the scans expect and what they work with.&lt;&#x2F;p&gt;
1043&lt;p&gt;Now it&#x27;s time to introduce support for different types, like &lt;code&gt;f32&lt;&#x2F;code&gt;, &lt;code&gt;i64&lt;&#x2F;code&gt;, or even more atypical ones, like arbitrary sequences of bytes (think of strings) or even numbers in big-endian.&lt;&#x2F;p&gt;
1044&lt;p&gt;Tighten your belt, because this post is quite the ride. Let&#x27;s get right into it!&lt;&#x2F;p&gt;
1045&lt;h2 id=&quot;floating-points&quot;&gt;Floating points&lt;&#x2F;h2&gt;
1046&lt;details open&gt;&lt;summary&gt;Cheat Engine Tutorial: Step 4&lt;&#x2F;summary&gt;
1047&lt;blockquote&gt;
1048&lt;p&gt;In the previous tutorial we used bytes to scan, but some games store information in so called &#x27;floating point&#x27; notations.
1049(probably to prevent simple memory scanners from finding it the easy way). A floating point is a value with some digits behind the point. (like 5.12 or 11321.1)&lt;&#x2F;p&gt;
1050&lt;p&gt;Below you see your health and ammo. Both are stored as Floating point notations, but health is stored as a float and ammo is stored as a double.
1051Click on hit me to lose some health, and on shoot to decrease your ammo with 0.5&lt;&#x2F;p&gt;
1052&lt;p&gt;You have to set BOTH values to 5000 or higher to proceed.&lt;&#x2F;p&gt;
1053&lt;p&gt;Exact value scan will work fine here, but you may want to experiment with other types too.&lt;&#x2F;p&gt;
1054&lt;p&gt;Hint: It is recommended to disable &amp;quot;Fast Scan&amp;quot; for type double&lt;&#x2F;p&gt;
1055&lt;&#x2F;blockquote&gt;
1056&lt;&#x2F;details&gt;
1057&lt;h2 id=&quot;generic-values&quot;&gt;Generic values&lt;&#x2F;h2&gt;
1058&lt;p&gt;The &lt;code&gt;Value&lt;&#x2F;code&gt; enumeration holds scanned values, and is currently hardcoded to store &lt;code&gt;i32&lt;&#x2F;code&gt;. The &lt;code&gt;Scan&lt;&#x2F;code&gt; type also holds a value, the value we want to scan for. Changing it to support other types is trivial:&lt;&#x2F;p&gt;
1059&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub enum Scan&amp;lt;T&amp;gt; {
1060    Exact(T),
1061    Unknown,
1062    Decreased,
1063    &#x2F;&#x2F; ...other variants...
1064}
1065
1066pub enum Value&amp;lt;T&amp;gt; {
1067    Exact(T),
1068    AnyWithin(Vec&amp;lt;u8&amp;gt;),
1069}
1070&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1071&lt;p&gt;&lt;code&gt;AnyWithin&lt;&#x2F;code&gt; is the raw memory, and &lt;code&gt;T&lt;&#x2F;code&gt; can be interpreted from any sequence of bytes thanks to our friend &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;std&#x2F;mem&#x2F;fn.transmute.html&quot;&gt;&lt;code&gt;mem::transmute&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. This change alone is enough to store an arbitrary &lt;code&gt;T&lt;&#x2F;code&gt;! So we&#x27;re done now? Not really, no.&lt;&#x2F;p&gt;
1072&lt;p&gt;First of all, we need to update all the places where &lt;code&gt;Scan&lt;&#x2F;code&gt; or &lt;code&gt;Value&lt;&#x2F;code&gt; are used. Our first stop is the scanned &lt;code&gt;Region&lt;&#x2F;code&gt;, which holds the found &lt;code&gt;Value&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
1073&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub struct Region&amp;lt;T&amp;gt; {
1074    pub info: MEMORY_BASIC_INFORMATION,
1075    pub locations: CandidateLocations,
1076    pub value: Value&amp;lt;T&amp;gt;,
1077}
1078&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1079&lt;p&gt;Then, we need to update everywhere &lt;code&gt;Region&lt;&#x2F;code&gt; is used, and on and on… All in all this process is just repeating &lt;code&gt;cargo check&lt;&#x2F;code&gt;, letting the compiler vent on you, and taking good care of it by fixing the errors. It&#x27;s quite reassuring to know you will not miss a single place. Thank you, compiler!&lt;&#x2F;p&gt;
1080&lt;p&gt;But wait, how could scanning for a decreased value work for any &lt;code&gt;T&lt;&#x2F;code&gt;? The type is not &lt;code&gt;Ord&lt;&#x2F;code&gt;, we should add some trait bounds. And also, what happens if the type is not &lt;code&gt;Copy&lt;&#x2F;code&gt;? It could implement &lt;code&gt;Drop&lt;&#x2F;code&gt;&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;, and we will be transmuting from raw bytes, which would trigger the &lt;code&gt;Drop&lt;&#x2F;code&gt; implementation when we&#x27;re done with the value! Not memory safe at all! And how could we possibly cast raw memory to the type without knowing its siz– oh nevermind, &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;std&#x2F;marker&#x2F;trait.Sized.html&quot;&gt;&lt;code&gt;T&lt;&#x2F;code&gt; is already &lt;code&gt;Sized&lt;&#x2F;code&gt; by default&lt;&#x2F;a&gt;. But anyway, we need the other bounds.&lt;&#x2F;p&gt;
1081&lt;p&gt;In order to not repeat ourselves, we will implement a new &lt;code&gt;trait&lt;&#x2F;code&gt;, let&#x27;s say &lt;code&gt;Scannable&lt;&#x2F;code&gt;, which requires all other bounds:&lt;&#x2F;p&gt;
1082&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub trait Scannable: Copy + PartialEq + PartialOrd {}
1083
1084impl&amp;lt;T: Copy + PartialEq + PartialOrd&amp;gt; Scannable for T {}
1085&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1086&lt;p&gt;And fix our definitions:&lt;&#x2F;p&gt;
1087&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub enum Scan&amp;lt;T: Scannable&amp;gt; { ... }
1088pub enum Value&amp;lt;T: Scannable&amp;gt; { ... }
1089pub struct Region&amp;lt;T: Scannable&amp;gt; { ... }
1090
1091&#x2F;&#x2F; ...and the many other places referring to T
1092&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1093&lt;p&gt;Every type which is &lt;code&gt;Copy&lt;&#x2F;code&gt;, &lt;code&gt;PartialEq&lt;&#x2F;code&gt; and &lt;code&gt;PartialOrd&lt;&#x2F;code&gt; can be scanned over&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;, because we &lt;code&gt;impl Scan for T&lt;&#x2F;code&gt; where the bounds are met. Unfortunately, we cannot require &lt;code&gt;Eq&lt;&#x2F;code&gt; or &lt;code&gt;Ord&lt;&#x2F;code&gt; because the floating point types do not implement it.&lt;&#x2F;p&gt;
1094&lt;h2 id=&quot;transmuting-memory&quot;&gt;Transmuting memory&lt;&#x2F;h2&gt;
1095&lt;p&gt;Also known as reinterpreting a bunch of bytes as something else, or perhaps it stands for &amp;quot;summoning the demon&amp;quot;:&lt;&#x2F;p&gt;
1096&lt;blockquote&gt;
1097&lt;p&gt;&lt;code&gt;transmute&lt;&#x2F;code&gt; is &lt;strong&gt;incredibly&lt;&#x2F;strong&gt; unsafe. There are a vast number of ways to cause &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;reference&#x2F;behavior-considered-undefined.html&quot;&gt;undefined behavior&lt;&#x2F;a&gt; with this function. &lt;code&gt;transmute&lt;&#x2F;code&gt; should be the absolute last resort.&lt;&#x2F;p&gt;
1098&lt;&#x2F;blockquote&gt;
1099&lt;p&gt;Types like &lt;code&gt;i32&lt;&#x2F;code&gt; define methods such as &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;std&#x2F;primitive.i32.html#method.from_ne_bytes&quot;&gt;&lt;code&gt;from_ne_bytes&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;std&#x2F;primitive.i32.html#method.to_ne_bytes&quot;&gt;&lt;code&gt;to_ne_bytes&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; which convert raw bytes from and into its native representation. This is all really nice, but unfortunately, there&#x27;s no standard trait in the Rust&#x27;s standard library to &amp;quot;interpret a type &lt;code&gt;T&lt;&#x2F;code&gt; as the byte sequence of its native representation&amp;quot;. &lt;code&gt;transmute&lt;&#x2F;code&gt;, however, does exist, and similar to any other &lt;code&gt;unsafe&lt;&#x2F;code&gt; function, it&#x27;s safe to call &lt;strong&gt;as long as we respect its invariants&lt;&#x2F;strong&gt;. What are these invariants&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;?&lt;&#x2F;p&gt;
1100&lt;blockquote&gt;
1101&lt;p&gt;Both types must have the same size&lt;&#x2F;p&gt;
1102&lt;&#x2F;blockquote&gt;
1103&lt;p&gt;Okay, we can just assert that the window length matches the type&#x27;s length. What else?&lt;&#x2F;p&gt;
1104&lt;blockquote&gt;
1105&lt;p&gt;Neither the original, nor the result, may be an &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;nomicon&#x2F;what-unsafe-does.html&quot;&gt;invalid value&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
1106&lt;&#x2F;blockquote&gt;
1107&lt;p&gt;What&#x27;s an invalid value?&lt;&#x2F;p&gt;
1108&lt;blockquote&gt;
1109&lt;ul&gt;
1110&lt;li&gt;a &lt;code&gt;bool&lt;&#x2F;code&gt; that isn&#x27;t 0 or 1&lt;&#x2F;li&gt;
1111&lt;li&gt;an &lt;code&gt;enum&lt;&#x2F;code&gt; with an invalid discriminant&lt;&#x2F;li&gt;
1112&lt;li&gt;a null &lt;code&gt;fn&lt;&#x2F;code&gt; pointer&lt;&#x2F;li&gt;
1113&lt;li&gt;a &lt;code&gt;char&lt;&#x2F;code&gt; outside the ranges [0x0, 0xD7FF] and [0xE000, 0x10FFFF]&lt;&#x2F;li&gt;
1114&lt;li&gt;a &lt;code&gt;!&lt;&#x2F;code&gt; (all values are invalid for this type)&lt;&#x2F;li&gt;
1115&lt;li&gt;an integer (&lt;code&gt;i*&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;u*&lt;&#x2F;code&gt;), floating point value (&lt;code&gt;f*&lt;&#x2F;code&gt;), or raw pointer read from uninitialized memory, or uninitialized memory in a &lt;code&gt;str&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
1116&lt;li&gt;a reference&#x2F;&lt;code&gt;Box&lt;&#x2F;code&gt; that is dangling, unaligned, or points to an invalid value.&lt;&#x2F;li&gt;
1117&lt;li&gt;a wide reference, &lt;code&gt;Box&lt;&#x2F;code&gt;, or raw pointer that has invalid metadata:
1118&lt;ul&gt;
1119&lt;li&gt;&lt;code&gt;dyn Trait&lt;&#x2F;code&gt; metadata is invalid if it is not a pointer to a vtable for &lt;code&gt;Trait&lt;&#x2F;code&gt; that matches the actual dynamic trait the pointer or reference points to&lt;&#x2F;li&gt;
1120&lt;li&gt;slice metadata is invalid if the length is not a valid &lt;code&gt;usize&lt;&#x2F;code&gt; (i.e., it must not be read from uninitialized memory)&lt;&#x2F;li&gt;
1121&lt;&#x2F;ul&gt;
1122&lt;&#x2F;li&gt;
1123&lt;li&gt;a type with custom invalid values that is one of those values, such as a &lt;code&gt;NonNull&lt;&#x2F;code&gt; that is null. (Requesting custom invalid values is an unstable feature, but some stable libstd types, like &lt;code&gt;NonNull&lt;&#x2F;code&gt;, make use of it.)&lt;&#x2F;li&gt;
1124&lt;&#x2F;ul&gt;
1125&lt;&#x2F;blockquote&gt;
1126&lt;p&gt;Okay, that&#x27;s actually an awful lot. Types like &lt;code&gt;bool&lt;&#x2F;code&gt; implement all the trait bounds we defined, and it would be insta-UB to ever try to cast them from arbitrary bytes. The same goes for &lt;code&gt;char&lt;&#x2F;code&gt;, and all &lt;code&gt;enum&lt;&#x2F;code&gt; are out of our control, too. At least we&#x27;re safe on the &amp;quot;memory is initialized&amp;quot; front.&lt;&#x2F;p&gt;
1127&lt;p&gt;Dang it, I really wanted to use &lt;code&gt;transmute&lt;&#x2F;code&gt;! But if we were to use it for arbitrary types, it would trigger undefined behaviour sooner than later.&lt;&#x2F;p&gt;
1128&lt;p&gt;We have several options here:&lt;&#x2F;p&gt;
1129&lt;ul&gt;
1130&lt;li&gt;Make it an &lt;code&gt;unsafe trait&lt;&#x2F;code&gt;. Implementors will be responsible for ensuring that the type they&#x27;re implementing it for can be safely transmuted from and into.&lt;&#x2F;li&gt;
1131&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;rust-lang.github.io&#x2F;api-guidelines&#x2F;future-proofing.html&quot;&gt;Seal the &lt;code&gt;trait&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and implement it only for types we know are safe&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#4&quot;&gt;4&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;, like &lt;code&gt;i32&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
1132&lt;li&gt;Add methods to the &lt;code&gt;trait&lt;&#x2F;code&gt; definition that do the conversion of the type into its native representation.&lt;&#x2F;li&gt;
1133&lt;&#x2F;ul&gt;
1134&lt;p&gt;We will go with the first option&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#5&quot;&gt;5&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;, because I really want to use &lt;code&gt;transmute&lt;&#x2F;code&gt;, and I want users to be able to implement the trait on their own types.&lt;&#x2F;p&gt;
1135&lt;p&gt;In any case, we need to change our &lt;code&gt;impl&lt;&#x2F;code&gt; to something more specific, in order to prevent it from automatically implementing the trait for types for which their memory representation has invalid values. So we get rid of this:&lt;&#x2F;p&gt;
1136&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub trait Scannable: Copy + PartialEq + PartialOrd {}
1137
1138impl&amp;lt;T: Copy + PartialEq + PartialOrd&amp;gt; Scannable for T {}
1139&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1140&lt;p&gt;And replace it with this:&lt;&#x2F;p&gt;
1141&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub unsafe trait Scannable: Copy + PartialEq + PartialOrd {}
1142
1143macro_rules! impl_many {
1144    ( unsafe impl $trait:tt for $( $ty:ty ),* ) =&amp;gt; {
1145        $( unsafe impl $trait for $ty {} )*
1146    };
1147}
1148
1149&#x2F;&#x2F; SAFETY: all these types respect `Scannable` invariants.
1150impl_many!(unsafe impl Scannable for i8, u8, i16, u16, i32, u32, i64, u64, f32, f64);
1151&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1152&lt;p&gt;Making a small macro for things like these is super useful. You could of course write &lt;code&gt;unsafe impl Scannable for T&lt;&#x2F;code&gt; for all ten &lt;code&gt;T&lt;&#x2F;code&gt; as well, but that introduces even more &lt;code&gt;unsafe&lt;&#x2F;code&gt; to read. Last but not least, let&#x27;s replace the hardcoded &lt;code&gt;i32::from_ne_bytes&lt;&#x2F;code&gt; and &lt;code&gt;i32::to_ne_bytes&lt;&#x2F;code&gt; with &lt;code&gt;mem::transmute&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
1153&lt;p&gt;All the &lt;code&gt;windows(4)&lt;&#x2F;code&gt; need to be replaced with &lt;code&gt;windows(mem::size_of::&amp;lt;T&amp;gt;())&lt;&#x2F;code&gt; because the size may no longer be &lt;code&gt;4&lt;&#x2F;code&gt;. All the &lt;code&gt;i32::from_ne_bytes(...)&lt;&#x2F;code&gt; need to be replaced with &lt;code&gt;mem::transmute::&amp;lt;_, T&amp;gt;(...)&lt;&#x2F;code&gt;. We explicitly write out &lt;code&gt;T&lt;&#x2F;code&gt; to make sure the compiler doesn&#x27;t accidentally infer something we didn&#x27;t intend.&lt;&#x2F;p&gt;
1154&lt;p&gt;And… it doesn&#x27;t work at all. We&#x27;re working with byte slices of arbitrary length. We cannot transmute a &lt;code&gt;&amp;amp;[]&lt;&#x2F;code&gt; type, which is 16 bytes (8 for the pointer and 8 for the length), to our &lt;code&gt;T&lt;&#x2F;code&gt;. My plan to use transmute can&#x27;t possibly work here. Sigh.&lt;&#x2F;p&gt;
1155&lt;h2 id=&quot;not-quite-transmuting-memory&quot;&gt;Not quite transmuting memory&lt;&#x2F;h2&gt;
1156&lt;p&gt;Okay, we can&#x27;t transmute, because we don&#x27;t have a sized value, we only have a slice of bytes pointing somewhere else. What we &lt;em&gt;could&lt;&#x2F;em&gt; do is reinterpret the pointer to those bytes as a different type, and then dereference it! This is still a form of &amp;quot;transmutation&amp;quot;, just without using &lt;code&gt;transmute&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
1157&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let value = unsafe { *(window.as_ptr() as *const T) };
1158&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1159&lt;p&gt;Woop! You can compile this and test it out on the step 2 and 3 of the tutorial, using &lt;code&gt;i32&lt;&#x2F;code&gt;, and it will still work! Something troubles me, though. Can you see what it is?&lt;&#x2F;p&gt;
1160&lt;p&gt;When we talked about invalid values, it had a note about unaligned references:&lt;&#x2F;p&gt;
1161&lt;blockquote&gt;
1162&lt;p&gt;a reference&#x2F;&lt;code&gt;Box&lt;&#x2F;code&gt; that is dangling, unaligned, or points to an invalid value.&lt;&#x2F;p&gt;
1163&lt;&#x2F;blockquote&gt;
1164&lt;p&gt;Our &lt;code&gt;window&lt;&#x2F;code&gt; is essentially a reference to &lt;code&gt;T&lt;&#x2F;code&gt;. The only difference is we&#x27;re working at the pointer level, but they&#x27;re pretty much references. Let&#x27;s see what the documentation for &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;primitive.pointer.html&quot;&gt;&lt;code&gt;pointer&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; has to say as well, since we&#x27;re dereferencing pointers:&lt;&#x2F;p&gt;
1165&lt;blockquote&gt;
1166&lt;p&gt;when a raw pointer is dereferenced (using the &lt;code&gt;*&lt;&#x2F;code&gt; operator), it must be non-null and aligned.&lt;&#x2F;p&gt;
1167&lt;&#x2F;blockquote&gt;
1168&lt;p&gt;It must be aligned. The only reason why our data is aligned is because we are also performing a &amp;quot;fast scan&amp;quot;, so we only look at aligned locations. This is a time bomb waiting to blow up. Is there any other way to &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;ptr&#x2F;fn.read.html&quot;&gt;&lt;code&gt;read&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; from a pointer which is safer?&lt;&#x2F;p&gt;
1169&lt;blockquote&gt;
1170&lt;p&gt;&lt;code&gt;src&lt;&#x2F;code&gt; must be properly aligned. Use &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;std&#x2F;ptr&#x2F;fn.read_unaligned.html&quot;&gt;&lt;code&gt;read_unaligned&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; if this is not the case.&lt;&#x2F;p&gt;
1171&lt;&#x2F;blockquote&gt;
1172&lt;p&gt;Bingo! Both &lt;code&gt;read&lt;&#x2F;code&gt; and &lt;code&gt;read_unaligned&lt;&#x2F;code&gt;, unlike dereferencing the pointer, will perform a copy, but if it can make the code less prone to blowing up, I&#x27;ll take it&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#6&quot;&gt;6&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. Let&#x27;s change the code one more time:&lt;&#x2F;p&gt;
1173&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let current = unsafe { window.as_ptr().cast::&amp;lt;T&amp;gt;().read_unaligned() };
1174&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1175&lt;p&gt;I prefer to avoid type annotations in variables where possible, which is why I use the &lt;a href=&quot;https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;3fimgp&#x2F;why_double_colon_rather_that_dot&#x2F;ctozkd0&#x2F;&quot;&gt;turbofish&lt;&#x2F;a&gt; so often. You can get rid of the cast and use a type annotation instead, but make sure the type is known, otherwise it will think it&#x27;s &lt;code&gt;u8&lt;&#x2F;code&gt; because &lt;code&gt;window&lt;&#x2F;code&gt; is a &lt;code&gt;&amp;amp;[u8]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
1176&lt;p&gt;Now, this is all cool and good. You can replace &lt;code&gt;i32&lt;&#x2F;code&gt; with &lt;code&gt;f32&lt;&#x2F;code&gt; for &lt;code&gt;T&lt;&#x2F;code&gt; and you&#x27;ll be able to get halfway done with the step 4 of Cheat Engine&#x27;s tutorial. Unfortunately, as it is, this code is not enough to complete step 4 with exact scans&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#7&quot;&gt;7&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. You see, comparing floating point values is not as simple as checking for bitwise equality. We were actually really lucky that the &lt;code&gt;f32&lt;&#x2F;code&gt; part works! But the values in the &lt;code&gt;f64&lt;&#x2F;code&gt; part are not as precise as our inputs, so our exact scan fails.&lt;&#x2F;p&gt;
1177&lt;p&gt;Using a fixed type parameter is pretty limiting as well. On the one hand, it is nice that, if you scan for &lt;code&gt;i32&lt;&#x2F;code&gt;, the compiler statically guarantees that subsequent scans will also happen on &lt;code&gt;i32&lt;&#x2F;code&gt; and thus be compatible. On the other, this requires us to know the type at compile time, which for an interactive program, is not possible. While we &lt;em&gt;could&lt;&#x2F;em&gt; create different methods for each supported type and, at runtime, decide to which we should jump, I am not satisfied with that solution. It also means we can&#x27;t switch from scanning an &lt;code&gt;u32&lt;&#x2F;code&gt; to an &lt;code&gt;i32&lt;&#x2F;code&gt;, for whatever reason.&lt;&#x2F;p&gt;
1178&lt;p&gt;So we need to work around this once more.&lt;&#x2F;p&gt;
1179&lt;h2 id=&quot;rethinking-the-scans&quot;&gt;Rethinking the scans&lt;&#x2F;h2&gt;
1180&lt;p&gt;What does our scanning function need, really? It needs a way to compare two chunks of memory as being equal or not (as we have seen, this isn&#x27;t trivial with types such as floating point numbers) and, for other types of scans, it needs to be able to produce an ordering, or calculate a difference.&lt;&#x2F;p&gt;
1181&lt;p&gt;Instead of having a our trait require the bounds &lt;code&gt;PartialEq&lt;&#x2F;code&gt; and &lt;code&gt;PartialOrd&lt;&#x2F;code&gt;, we can define our own methods to compare &lt;code&gt;Self&lt;&#x2F;code&gt; with &lt;code&gt;&amp;amp;[u8]&lt;&#x2F;code&gt;. It still should be &lt;code&gt;Clone&lt;&#x2F;code&gt;, so we can pass it around without worrying about lifetimes:&lt;&#x2F;p&gt;
1182&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&#x2F;&#x2F; Callers must `assert_eq!(memory.len(), mem::size_of::&amp;lt;Self&amp;gt;())`.
1183unsafe fn eq(&amp;amp;self, memory: &amp;amp;[u8]) -&amp;gt; bool;
1184unsafe fn cmp(&amp;amp;self, memory: &amp;amp;[u8]) -&amp;gt; Ordering;
1185&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1186&lt;p&gt;This can be trivially implemented for all integer types:&lt;&#x2F;p&gt;
1187&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;macro_rules! impl_scannable_for_int {
1188    ( $( $ty:ty ),* ) =&amp;gt; {
1189        $(
1190            &#x2F;&#x2F; SAFETY: caller is responsible to `assert_eq!(memory.len(), mem::size_of::&amp;lt;T&amp;gt;())`
1191            impl Scannable for $ty {
1192                unsafe fn eq(&amp;amp;self, memory: &amp;amp;[u8]) -&amp;gt; bool {
1193                    let other = unsafe { memory.as_ptr().cast::&amp;lt;$ty&amp;gt;().read_unaligned() };
1194                    *self == other
1195                }
1196
1197                unsafe fn cmp(&amp;amp;self, memory: &amp;amp;[u8]) -&amp;gt; Ordering {
1198                    let other = unsafe { memory.as_ptr().cast::&amp;lt;$ty&amp;gt;().read_unaligned() };
1199                    &amp;lt;$ty as Ord&amp;gt;::cmp(self, &amp;amp;other)
1200                }
1201            }
1202        )*
1203    };
1204}
1205
1206impl_scannable_for_int!(i8, u8, i16, u16, i32, u32, i64, u64);
1207&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1208&lt;p&gt;The funny &lt;code&gt;&amp;lt;$ty as Ord&amp;gt;&lt;&#x2F;code&gt; is because I decided to call the method &lt;code&gt;Scannable::cmp&lt;&#x2F;code&gt;, so I have to disambiguate between it and &lt;code&gt;Ord::cmp&lt;&#x2F;code&gt;. We can go ahead and update the code using &lt;code&gt;Scannable&lt;&#x2F;code&gt; to use these new functions instead.&lt;&#x2F;p&gt;
1209&lt;p&gt;Now, you may have noticed I only implemented it for the integer types. That&#x27;s because floats need some extra care. Unfortunately, floating point types do not have any form of &amp;quot;precision&amp;quot; embedded in them, so we can&#x27;t accurately say &amp;quot;compare these floats to the precision level the user specified&amp;quot;. What we can do, however, is drop a few bits from the mantissa, so &amp;quot;relatively close&amp;quot; quantities are considered equal. It&#x27;s definitely not as good as comparing floats to the user&#x27;s precision, but it will get the job done.&lt;&#x2F;p&gt;
1210&lt;p&gt;I&#x27;m going to arbitrarily say that we are okay comparing with &amp;quot;half&amp;quot; the precision. We can achieve that by masking half of the bits from the mantissa to zero:&lt;&#x2F;p&gt;
1211&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;
1212macro_rules! impl_scannable_for_float {
1213    ( $( $ty:ty : $int_ty:ty ),* ) =&amp;gt; {
1214        $(
1215            #[allow(unused_unsafe)] &#x2F;&#x2F; mind you, it is necessary
1216            impl Scannable for $ty {
1217                unsafe fn eq(&amp;amp;self, memory: &amp;amp;[u8]) -&amp;gt; bool {
1218                    const MASK: $int_ty = !((1 &amp;lt;&amp;lt; (&amp;lt;$ty&amp;gt;::MANTISSA_DIGITS &#x2F; 2)) - 1);
1219
1220                    &#x2F;&#x2F; SAFETY: caller is responsible to `assert_eq!(memory.len(), mem::size_of::&amp;lt;T&amp;gt;())`
1221                    let other = unsafe { memory.as_ptr().cast::&amp;lt;$ty&amp;gt;().read_unaligned() };
1222                    let left = &amp;lt;$ty&amp;gt;::from_bits(self.to_bits() &amp;amp; MASK);
1223                    let right = &amp;lt;$ty&amp;gt;::from_bits(other.to_bits() &amp;amp; MASK);
1224                    left == right
1225                }
1226
1227                ...
1228            }
1229        )*
1230    };
1231}
1232
1233impl_scannable_for_float!(f32: u32, f64: u64);
1234&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1235&lt;p&gt;You may be wondering what&#x27;s up with that weird &lt;code&gt;MASK&lt;&#x2F;code&gt;. Let&#x27;s visualize it with a &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bfloat16_floating-point_format&quot;&gt;&lt;code&gt;f16&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. This type has 16 bits, 1 for sign, 5 for exponent, and 10 for the mantissa:&lt;&#x2F;p&gt;
1236&lt;pre&gt;&lt;code&gt;S EEEEE MMMMMMMMMM
1237&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1238&lt;p&gt;If we substitute the constant with the numeric value and operate:&lt;&#x2F;p&gt;
1239&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;!((1 &amp;lt;&amp;lt; (10 &#x2F; 2)) - 1)
1240!((1 &amp;lt;&amp;lt; 5) - 1)
1241!(0b00000000_00100000 - 1)
1242!(0b00000000_00011111)
12430b11111111_11100000
1244&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1245&lt;p&gt;So effectively, half of the mantisssa bit will be masked to 0. For the &lt;code&gt;f16&lt;&#x2F;code&gt; example, this makes us lose 5 bits of precision. Comparing two floating point values with their last five bits truncated is equivalent to checking if they are &amp;quot;roughly equal&amp;quot;!&lt;&#x2F;p&gt;
1246&lt;p&gt;When Cheat Engine scans for floating point values, several additional settings show, and one such option is &amp;quot;truncated&amp;quot;. I do not know if it behaves like this, but it might.&lt;&#x2F;p&gt;
1247&lt;p&gt;Let&#x27;s try this out:&lt;&#x2F;p&gt;
1248&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;#[test]
1249fn f32_roughly_eq() {
1250    let left = 0.25f32;
1251    let right = 0.25000123f32;
1252    let memory = unsafe { mem::transmute::&amp;lt;_, [u8; 4]&amp;gt;(right) };
1253    assert_ne!(left, right);
1254    assert!(unsafe { Scannable::eq(&amp;amp;left, &amp;amp;memory) });
1255}
1256&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1257&lt;pre&gt;&lt;code&gt;&amp;gt;cargo test f32_roughly_eq
1258
1259running 1 test
1260test scan::candidate_location_tests::f32_roughly_eq ... ok
1261&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1262&lt;p&gt;Huzzah! The &lt;code&gt;assert_ne!&lt;&#x2F;code&gt; makes sure that a normal comparision would fail, and then we &lt;code&gt;assert!&lt;&#x2F;code&gt; that our custom one passes the test. When the user performs an exact scan, the code will be more tolerant to the user&#x27;s less precise inputs, which overall should result in a nicer experience.&lt;&#x2F;p&gt;
1263&lt;h2 id=&quot;dynamically-sized-scans&quot;&gt;Dynamically sized scans&lt;&#x2F;h2&gt;
1264&lt;p&gt;The second problem we need to solve is the possibility of the size not being known at compile time&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#8&quot;&gt;8&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. While we can go as far as scanning over strings of a known length, this is rather limiting, because we need to know the length at compile time&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#9&quot;&gt;9&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. Heap allocated objects are another problem, because we don&#x27;t want to compare the memory representation of the stack object, but likely the memory where they point to (such as &lt;code&gt;String&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
1265&lt;p&gt;Instead of using &lt;code&gt;mem::size_of&lt;&#x2F;code&gt;, we can add a new method to our &lt;code&gt;Scannable&lt;&#x2F;code&gt;, &lt;code&gt;size&lt;&#x2F;code&gt;, which will tell us the size required of the memory view we&#x27;re comparing against:&lt;&#x2F;p&gt;
1266&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;unsafe impl Scannable {
1267    ...
1268
1269    fn size(&amp;amp;self) -&amp;gt; usize;
1270}
1271&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1272&lt;p&gt;It is &lt;code&gt;unsafe&lt;&#x2F;code&gt; to implement, because we are relying on the returned value to be truthful and unchanging. It should be safe to call, because it cannot have any invariants. Unfortunately, signaling &amp;quot;unsafe to implement&amp;quot; is done by marking the entire trait as &lt;code&gt;unsafe&lt;&#x2F;code&gt;, since &amp;quot;unsafe to call&amp;quot; is reserved for &lt;code&gt;unsafe fn&lt;&#x2F;code&gt;, and even though the rest of methods are not necessarily unsafe to implement, they&#x27;re treated as such.&lt;&#x2F;p&gt;
1273&lt;p&gt;At the moment, &lt;code&gt;Scannable&lt;&#x2F;code&gt; cannot be made into a trait object because it is &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;error-index.html#E0038&quot;&gt;not object safe&lt;&#x2F;a&gt;. This is caused by the &lt;code&gt;Clone&lt;&#x2F;code&gt; requirement on all &lt;code&gt;Scannable&lt;&#x2F;code&gt; object, which in turn needs the types to be &lt;code&gt;Sized&lt;&#x2F;code&gt; because &lt;code&gt;clone&lt;&#x2F;code&gt; returns &lt;code&gt;Self&lt;&#x2F;code&gt;. Because of this, the size must be known.&lt;&#x2F;p&gt;
1274&lt;p&gt;However, we &lt;em&gt;can&lt;&#x2F;em&gt; move the &lt;code&gt;Clone&lt;&#x2F;code&gt; requirement to the methods that need it! This way, &lt;code&gt;Scannable&lt;&#x2F;code&gt; can remain object safe, enabling us to do the following:&lt;&#x2F;p&gt;
1275&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;unsafe impl&amp;lt;T: AsRef&amp;lt;dyn Scannable&amp;gt; + AsMut&amp;lt;dyn Scannable&amp;gt;&amp;gt; Scannable for T {
1276    unsafe fn eq(&amp;amp;self, memory: &amp;amp;[u8]) -&amp;gt; bool {
1277        self.as_ref().eq(memory)
1278    }
1279
1280    unsafe fn cmp(&amp;amp;self, memory: &amp;amp;[u8]) -&amp;gt; Ordering {
1281        self.as_ref().cmp(memory)
1282    }
1283
1284    fn mem_view(&amp;amp;self) -&amp;gt; &amp;amp;[u8] {
1285        self.as_ref().mem_view()
1286    }
1287
1288    fn size(&amp;amp;self) -&amp;gt; usize {
1289        self.as_ref().size()
1290    }
1291}
1292&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1293&lt;p&gt;Any type which can be interpreted as a reference to &lt;code&gt;Scannable&lt;&#x2F;code&gt; is also a scannable! This enables us to perform scans over &lt;code&gt;Box&amp;lt;dyn i32&amp;gt;&lt;&#x2F;code&gt;, where the type is known at runtime! Or rather, it would, if &lt;code&gt;Box&amp;lt;dyn T&amp;gt;&lt;&#x2F;code&gt; implemented &lt;code&gt;Clone&lt;&#x2F;code&gt;, which it can&#x27;t&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#10&quot;&gt;10&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; because that&#x27;s what prompted this entire issue. Dang it! I can&#x27;t catch a breath today!&lt;&#x2F;p&gt;
1294&lt;p&gt;Okay, let&#x27;s step back. Why did we need our scannables to be clone in the first place? When we perform exact scans, we store the original value in the region, which we don&#x27;t own, so we clone it. But what if we &lt;em&gt;did&lt;&#x2F;em&gt; own the value? Instead of taking the &lt;code&gt;Scan&lt;&#x2F;code&gt; by reference, which holds &lt;code&gt;T: Scannable&lt;&#x2F;code&gt;, we could take it by value. If we get rid of all the &lt;code&gt;Clone&lt;&#x2F;code&gt; bounds and update &lt;code&gt;Scan::run&lt;&#x2F;code&gt; to take &lt;code&gt;self&lt;&#x2F;code&gt;, along with updating all the things that take a &lt;code&gt;Region&lt;&#x2F;code&gt; to take them by value as well, it should all work out.&lt;&#x2F;p&gt;
1295&lt;p&gt;But it does not. If we take &lt;code&gt;Scan&lt;&#x2F;code&gt; by value, with it not being &lt;code&gt;Clone&lt;&#x2F;code&gt;, we simply can&#x27;t use it to scan over multiple regions. After the first region, we have lost the &lt;code&gt;Scan&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
1296&lt;p&gt;Let&#x27;s take a second step back. We are scanning memory, and we want to compare memory, but we want to treat the memory with different semantics (for example, if we treat it as &lt;code&gt;f32&lt;&#x2F;code&gt;, we want to check for rough equality). Instead of storing the &lt;em&gt;value&lt;&#x2F;em&gt; itself, we could store its &lt;em&gt;memory representation&lt;&#x2F;em&gt;, and when we compare memory representations, we can do so under certain semantics.&lt;&#x2F;p&gt;
1297&lt;p&gt;First off, let&#x27;s revert getting rid of all &lt;code&gt;Clone&lt;&#x2F;code&gt;. Wherever we stored a &lt;code&gt;T&lt;&#x2F;code&gt;, we will now store a &lt;code&gt;Vec&amp;lt;u8&amp;gt;&lt;&#x2F;code&gt;. We will still use a type parameter to represent the &amp;quot;implementations of &lt;code&gt;Scannable&lt;&#x2F;code&gt;&amp;quot;. For this to work, our definitions need to use &lt;code&gt;T&lt;&#x2F;code&gt; somewhere, or else the compiler refuses to compile the code with error &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;error-index.html#E0392&quot;&gt;E0392&lt;&#x2F;a&gt;. For this, I will stick a &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;std&#x2F;marker&#x2F;struct.PhantomData.html&quot;&gt;&lt;code&gt;PhantomData&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; in the &lt;code&gt;Exact&lt;&#x2F;code&gt; variant. It&#x27;s a bit pointless to include it in all variants, and &lt;code&gt;Exact&lt;&#x2F;code&gt; seems the most appropriated:&lt;&#x2F;p&gt;
1298&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub enum Scan&amp;lt;T: Scannable&amp;gt; {
1299    Exact(Vec&amp;lt;u8&amp;gt;, PhantomData&amp;lt;T&amp;gt;),
1300    Unknown,
1301    ...
1302}
1303&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1304&lt;p&gt;This keeps in line with &lt;code&gt;Value&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
1305&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub enum Value&amp;lt;T: Scannable&amp;gt; {
1306    Exact(Vec&amp;lt;u8&amp;gt;, PhantomData&amp;lt;T&amp;gt;),
1307    ...
1308}
1309&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1310&lt;p&gt;Our &lt;code&gt;Scannable&lt;&#x2F;code&gt; will no longer work on &lt;code&gt;T&lt;&#x2F;code&gt; and &lt;code&gt;&amp;amp;[u8]&lt;&#x2F;code&gt;. Instead, it will work on two &lt;code&gt;&amp;amp;[u8]&lt;&#x2F;code&gt;. We will also need a way to interpret a &lt;code&gt;T&lt;&#x2F;code&gt; as &lt;code&gt;&amp;amp;[u8]&lt;&#x2F;code&gt;, which we can achieve with a new method, &lt;code&gt;mem_view&lt;&#x2F;code&gt;. This method interprets the raw memory representation of &lt;code&gt;self&lt;&#x2F;code&gt; as its raw bytes. It also lets us get rid of &lt;code&gt;size&lt;&#x2F;code&gt;, because we can simply do &lt;code&gt;mem_view().len()&lt;&#x2F;code&gt;. It&#x27;s still &lt;code&gt;unsafe&lt;&#x2F;code&gt; to implement, because it should return the same length every time:&lt;&#x2F;p&gt;
1311&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub unsafe trait Scannable {
1312    &#x2F;&#x2F; Callers must `assert_eq!(left.len(), right.len(), self.mem_view().len())`.
1313    unsafe fn eq(left: &amp;amp;[u8], right: &amp;amp;[u8]) -&amp;gt; bool;
1314    unsafe fn cmp(left: &amp;amp;[u8], right: &amp;amp;[u8]) -&amp;gt; Ordering;
1315    fn mem_view(&amp;amp;self) -&amp;gt; &amp;amp;[u8];
1316}
1317&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1318&lt;p&gt;But now we can&#x27;t use it in trait object, so the following no longer works:&lt;&#x2F;p&gt;
1319&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;unsafe impl&amp;lt;T: AsRef&amp;lt;dyn Scannable&amp;gt; + AsMut&amp;lt;dyn Scannable&amp;gt;&amp;gt; Scannable for T {
1320    ...
1321}
1322&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1323&lt;p&gt;Ugh! Well, to be fair, we no longer have a &amp;quot;scannable&amp;quot; at this point. It&#x27;s more like a scan mode that tells us how memory should be compared according to a certain type. Let&#x27;s split the trait into two: one for the scan mode, and other for &amp;quot;things which are scannable&amp;quot;:&lt;&#x2F;p&gt;
1324&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub trait ScanMode {
1325    unsafe fn eq(left: &amp;amp;[u8], right: &amp;amp;[u8]) -&amp;gt; bool;
1326    unsafe fn cmp(left: &amp;amp;[u8], right: &amp;amp;[u8]) -&amp;gt; Ordering;
1327}
1328
1329pub unsafe trait Scannable {
1330    type Mode: ScanMode;
1331
1332    fn mem_view(&amp;amp;self) -&amp;gt; &amp;amp;[u8];
1333}
1334&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1335&lt;p&gt;Note that we have an associated &lt;code&gt;type Mode&lt;&#x2F;code&gt; which contains the corresponding &lt;code&gt;ScanMode&lt;&#x2F;code&gt;. If we used a trait bound such as &lt;code&gt;Scannable: ScanMode&lt;&#x2F;code&gt;, we&#x27;d be back to square one: it would inherit the method definitions that don&#x27;t use &lt;code&gt;&amp;amp;self&lt;&#x2F;code&gt; and thus cannot be used as trait objects.&lt;&#x2F;p&gt;
1336&lt;p&gt;With these changes, it is possible to implement &lt;code&gt;Scannable&lt;&#x2F;code&gt; for any &lt;code&gt;dyn Scannable&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
1337&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;unsafe impl&amp;lt;T: ScanMode + AsRef&amp;lt;dyn Scannable&amp;lt;Mode = Self&amp;gt;&amp;gt;&amp;gt; Scannable for T {
1338    type Mode = Self;
1339
1340    fn mem_view(&amp;amp;self) -&amp;gt; &amp;amp;[u8] {
1341        self.as_ref().mem_view()
1342    }
1343}
1344&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1345&lt;p&gt;We do have to adjust a few places of the code to account for both &lt;code&gt;Scannable&lt;&#x2F;code&gt; and &lt;code&gt;ScanMode&lt;&#x2F;code&gt;, but all in all, it&#x27;s pretty straightforward. Things like &lt;code&gt;Value&lt;&#x2F;code&gt; don&#x27;t need to store the &lt;code&gt;Scannable&lt;&#x2F;code&gt; anymore, just a &lt;code&gt;Vec&amp;lt;u8&amp;gt;&lt;&#x2F;code&gt;. It also doesn&#x27;t need the &lt;code&gt;ScanMode&lt;&#x2F;code&gt;, because it&#x27;s not going to be scanning anything on its own. This applies transitively to &lt;code&gt;Region&lt;&#x2F;code&gt; which was holding a &lt;code&gt;Value&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
1346&lt;p&gt;&lt;code&gt;Value&lt;&#x2F;code&gt; &lt;em&gt;does&lt;&#x2F;em&gt; need to be updated to store the size of the region we are scanning for, however, because we need that information when running a subsequent scan. For all &lt;code&gt;Scan&lt;&#x2F;code&gt; that don&#x27;t have a explicit thing to scan for (like &lt;code&gt;Decreased&lt;&#x2F;code&gt;), the &lt;code&gt;size&lt;&#x2F;code&gt; also needs to be stored in them.&lt;&#x2F;p&gt;
1347&lt;p&gt;Despite all our efforts, we&#x27;re still unable to return an &lt;code&gt;Scannable&lt;&#x2F;code&gt; chosen at runtime.&lt;&#x2F;p&gt;
1348&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;fn prompt_user_for_scan() -&amp;gt; Scan&amp;lt;Box&amp;lt;dyn Scannable&amp;lt;Mode = ???&amp;gt;&amp;gt;&amp;gt; {
1349    todo!()
1350}
1351&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1352&lt;p&gt;As far as I can tell, there&#x27;s simply no way to specify that type. We want to return a type which is scannable, which has itself (which is also a &lt;code&gt;ScanMode&lt;&#x2F;code&gt;) as the corresponding mode. Even if we just tried to return the mode, we simply can&#x27;t, because it&#x27;s not object-safe. Is this the end of the road?&lt;&#x2F;p&gt;
1353&lt;h2 id=&quot;specifying-the-scan-mode&quot;&gt;Specifying the scan mode&lt;&#x2F;h2&gt;
1354&lt;p&gt;We need a way to pass an arbitrary scan mode to our &lt;code&gt;Scan&lt;&#x2F;code&gt;. This scan mode should go in tandem with &lt;code&gt;Scannable&lt;&#x2F;code&gt; types, because it would be unsafe otherwise. We&#x27;ve seen that using a type just doesn&#x27;t cut it. What else can we do?&lt;&#x2F;p&gt;
1355&lt;p&gt;Using an enumeration is a no-go, because I want users to be able to extend it further. I also would like to avoid having to update the &lt;code&gt;enum&lt;&#x2F;code&gt; and all the matches every time I come up with a different type combination. And it could get pretty complicated if I ever built something dynamically, such as letting the user combine different scans in one pass.&lt;&#x2F;p&gt;
1356&lt;p&gt;So what if we make &lt;code&gt;Scannable&lt;&#x2F;code&gt; return a value that implements the functions we need?&lt;&#x2F;p&gt;
1357&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub struct ScanMode {
1358    eq: unsafe fn(left: &amp;amp;[u8], right: &amp;amp;[u8]) -&amp;gt; bool,
1359    cmp: unsafe fn(left: &amp;amp;[u8], right: &amp;amp;[u8]) -&amp;gt; Ordering,
1360}
1361&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1362&lt;p&gt;It&#x27;s definitely… non-conventional. But hey, now we&#x27;re left with the &lt;code&gt;Scannable&lt;&#x2F;code&gt; trait, which is object-safe, and does not have any type parameters!&lt;&#x2F;p&gt;
1363&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub unsafe trait Scannable {
1364    fn mem_view(&amp;amp;self) -&amp;gt; &amp;amp;[u8];
1365    fn scan_mode(&amp;amp;self) -&amp;gt; ScanMode;
1366}
1367&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1368&lt;p&gt;It is a bit weird, but defining local functions and using those in the returned value is a nice way to keep things properly scoped:&lt;&#x2F;p&gt;
1369&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;macro_rules! impl_scannable_for_int {
1370    ( $( $ty:ty ),* ) =&amp;gt; {
1371        $(
1372            unsafe impl Scannable for $ty {
1373                fn mem_view(&amp;amp;self) -&amp;gt; &amp;amp;[u8] {
1374                    unsafe { std::slice::from_raw_parts(self as *const _ as *const u8, mem::size_of::&amp;lt;$ty&amp;gt;()) }
1375                }
1376
1377                fn scan_mode(&amp;amp;self) -&amp;gt; ScanMode {
1378                    unsafe fn eq(left: &amp;amp;[u8], right: &amp;amp;[u8]) -&amp;gt; bool {
1379                        ...
1380                    }
1381
1382                    unsafe fn cmp(left: &amp;amp;[u8], right: &amp;amp;[u8]) -&amp;gt; Ordering {
1383                        ...
1384                    }
1385
1386                    ScanMode { eq, cmp }
1387                }
1388            }
1389        )*
1390    };
1391}
1392&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1393&lt;p&gt;Our &lt;code&gt;Scan&lt;&#x2F;code&gt; needs to store the &lt;code&gt;Scannable&lt;&#x2F;code&gt; type, and not just the memory, once again. For variants that don&#x27;t need any value, they can store the &lt;code&gt;ScanMode&lt;&#x2F;code&gt; and size instead.&lt;&#x2F;p&gt;
1394&lt;p&gt;Does this solution work? Yes! It&#x27;s possible to return a &lt;code&gt;Box&amp;lt;dyn Scannable&amp;gt;&lt;&#x2F;code&gt; from a function, and underneath, it may be using any type which is &lt;code&gt;Scannable&lt;&#x2F;code&gt;. Is this the best solution? Well, that&#x27;s hard to say. This is &lt;em&gt;one&lt;&#x2F;em&gt; of the possible solutions.&lt;&#x2F;p&gt;
1395&lt;p&gt;We have been going around in circles for quite some time now, so I&#x27;ll leave it there. It&#x27;s a solution, which may not be pretty, but it works. With these changes, the code is capable of completing all of the steps in the Cheat Engine tutorial up until point!&lt;&#x2F;p&gt;
1396&lt;h2 id=&quot;finale&quot;&gt;Finale&lt;&#x2F;h2&gt;
1397&lt;p&gt;If there&#x27;s one lesson to learn from this post, it&#x27;s that there is often no single correct solution to a problem. We could have approached the scan types in many, many ways (and we tried quite a few!), but in the end, choosing one option or the other comes down to your (sometimes self-imposed) requirements.&lt;&#x2F;p&gt;
1398&lt;p&gt;You may &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lonami&#x2F;memo&quot;&gt;obtain the code for this post&lt;&#x2F;a&gt; over at my GitHub. You can run &lt;code&gt;git checkout step4&lt;&#x2F;code&gt; after cloning the repository to get the right version of the code. The code has gone through a lot of iterations, and I&#x27;d still like to polish it a bit more, so it might slightly differ from the code presented in this entry.&lt;&#x2F;p&gt;
1399&lt;p&gt;If you feel adventurous, Cheat Engine has different options for scanning floating point types: &amp;quot;rounded (default)&amp;quot;, &amp;quot;rounded (extreme)&amp;quot;, and truncated. Optionally, it can scan for &amp;quot;simple values only&amp;quot;. You could go ahead and toy around with these!&lt;&#x2F;p&gt;
1400&lt;p&gt;We didn&#x27;t touch on types with different lengths, such as strings. You could support UTF-8, UTF-16, or arbitrary byte sequences. This post also didn&#x27;t cover scanning for multiple things at once, known as &amp;quot;groupscan commands&amp;quot;, although from what I can tell, these are just a nice way to scan for arbitrary byte sequences.&lt;&#x2F;p&gt;
1401&lt;p&gt;We also didn&#x27;t look into supporting different the same scan with different alignments. All these things may be worth exploring depending on your requirements. You could even get rid of such genericity and go with something way simpler. Supporting &lt;code&gt;i32&lt;&#x2F;code&gt;, &lt;code&gt;f32&lt;&#x2F;code&gt; and &lt;code&gt;f64&lt;&#x2F;code&gt; is enough to complete the Cheat Engine tutorial. But I wanted something more powerful, although my solution currently can&#x27;t scan for a sequence such as &amp;quot;exact type, unknown, exact matching the unknown&amp;quot;. So yeah.&lt;&#x2F;p&gt;
1402&lt;p&gt;In the &lt;a href=&quot;&#x2F;blog&#x2F;woce-5&quot;&gt;next post&lt;&#x2F;a&gt;, we&#x27;ll tackle the fifth step of the tutorial: Code finder. Cheat Engine attaches its debugger to the process for this one, and then replaces the instruction that performs the write with a different no-op so that nothing is written anymore. This will be quite the challenge!&lt;&#x2F;p&gt;
1403&lt;h3 id=&quot;footnotes&quot;&gt;Footnotes&lt;&#x2F;h3&gt;
1404&lt;div class=&quot;footnote-definition&quot; id=&quot;1&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;&#x2F;sup&gt;
1405&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;std&#x2F;ops&#x2F;trait.Drop.html#copy-and-drop-are-exclusive&quot;&gt;&lt;code&gt;Copy&lt;&#x2F;code&gt; and &lt;code&gt;Drop&lt;&#x2F;code&gt; are exclusive&lt;&#x2F;a&gt;. See also &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;error-index.html#E0184&quot;&gt;E0184&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
1406&lt;&#x2F;div&gt;
1407&lt;div class=&quot;footnote-definition&quot; id=&quot;2&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;2&lt;&#x2F;sup&gt;
1408&lt;p&gt;If you added more scan types that require additional bounds, make sure to add them too. For example, the &amp;quot;decreased by&amp;quot; scan requires the type to &lt;code&gt;impl Sub&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
1409&lt;&#x2F;div&gt;
1410&lt;div class=&quot;footnote-definition&quot; id=&quot;3&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;3&lt;&#x2F;sup&gt;
1411&lt;p&gt;This is a good time to remind you to read the documentation. It is of special importance when dealing with &lt;code&gt;unsafe&lt;&#x2F;code&gt; methods; I recommend reading it a couple times.&lt;&#x2F;p&gt;
1412&lt;&#x2F;div&gt;
1413&lt;div class=&quot;footnote-definition&quot; id=&quot;4&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;4&lt;&#x2F;sup&gt;
1414&lt;p&gt;Even with this option, it would not be a bad idea to make the trait &lt;code&gt;unsafe&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
1415&lt;&#x2F;div&gt;
1416&lt;div class=&quot;footnote-definition&quot; id=&quot;5&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;5&lt;&#x2F;sup&gt;
1417&lt;p&gt;Not for long. As we will find out later, this approach has its limitations.&lt;&#x2F;p&gt;
1418&lt;&#x2F;div&gt;
1419&lt;div class=&quot;footnote-definition&quot; id=&quot;6&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;6&lt;&#x2F;sup&gt;
1420&lt;p&gt;We can still perform the pointer dereference when we know it&#x27;s aligned. This would likely be an optimization, although it would definitely complicate the code more.&lt;&#x2F;p&gt;
1421&lt;&#x2F;div&gt;
1422&lt;div class=&quot;footnote-definition&quot; id=&quot;7&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;7&lt;&#x2F;sup&gt;
1423&lt;p&gt;It &lt;em&gt;would&lt;&#x2F;em&gt; work if you scanned for unknown values and then checked for decreased values repeatedly. But we can&#x27;t just leave exact scan broken!&lt;&#x2F;p&gt;
1424&lt;&#x2F;div&gt;
1425&lt;div class=&quot;footnote-definition&quot; id=&quot;8&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;8&lt;&#x2F;sup&gt;
1426&lt;p&gt;Unfortunately, this makes some optimizations harder or even impossible to perform. Providing specialized functions for types where the size is known at compile time could be worth doing. Programming is all tradeoffs.&lt;&#x2F;p&gt;
1427&lt;&#x2F;div&gt;
1428&lt;div class=&quot;footnote-definition&quot; id=&quot;9&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;9&lt;&#x2F;sup&gt;
1429&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blog.rust-lang.org&#x2F;2021&#x2F;02&#x2F;26&#x2F;const-generics-mvp-beta.html&quot;&gt;Rust 1.51&lt;&#x2F;a&gt;, which was not out at the time of writing, would make it a lot easier to allow scanning for fixed-length sequences of bytes, thanks to const generics.&lt;&#x2F;p&gt;
1430&lt;&#x2F;div&gt;
1431&lt;div class=&quot;footnote-definition&quot; id=&quot;10&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;10&lt;&#x2F;sup&gt;
1432&lt;p&gt;Workarounds do exist, such as &lt;a href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;dyn-clone&quot;&gt;dtolnay&#x27;s &lt;code&gt;dyn-clone&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. But I would rather not go that route.&lt;&#x2F;p&gt;
1433&lt;&#x2F;div&gt;
1434</content>
1435	</entry>
1436	<entry xml:lang="en">
1437		<title>Writing our own Cheat Engine: Unknown initial value</title>
1438		<published>2021-02-19T00:00:00+00:00</published>
1439		<updated>2021-02-19T00:00:00+00:00</updated>
1440		<link href="https://lonami.dev/blog/woce-3/" type="text/html"/>
1441		<id>https://lonami.dev/blog/woce-3/</id>
1442		<content type="html">&lt;p&gt;This is part 3 on the &lt;em&gt;Writing our own Cheat Engine&lt;&#x2F;em&gt; series:&lt;&#x2F;p&gt;
1443&lt;ul&gt;
1444&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-1&quot;&gt;Part 1: Introduction&lt;&#x2F;a&gt; (start here if you&#x27;re new to the series!)&lt;&#x2F;li&gt;
1445&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-2&quot;&gt;Part 2: Exact Value scanning&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
1446&lt;li&gt;Part 3: Unknown initial value&lt;&#x2F;li&gt;
1447&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-4&quot;&gt;Part 4: Floating points&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
1448&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-5&quot;&gt;Part 5: Code finder&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
1449&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-6&quot;&gt;Part 6: Pointers&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
1450&lt;&#x2F;ul&gt;
1451&lt;p&gt;In part 2 we left off with a bit of a cliff-hanger. Our little program is now able to scan for an exact value, remember the couple hundred addresses pointing to said value, and perform subsequent scans to narrow the list of addresses down until we&#x27;re left with a handful of them.&lt;&#x2F;p&gt;
1452&lt;p&gt;However, it is not always the case that you have an exact value to work with. The best you can do in these cases is guess what the software might be storing. For example, it could be a floating point for your current movement speed in a game, or an integer for your current health.&lt;&#x2F;p&gt;
1453&lt;p&gt;The problem with this is that there are far too many possible locations storing our desired value. If you count misaligned locations, this means there is a different location to address every single byte in memory. A program with one megabyte of memory already has a &lt;em&gt;million&lt;&#x2F;em&gt; of addresses. Clearly, we need to do better than performing one million memory reads&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;.&lt;&#x2F;p&gt;
1454&lt;p&gt;This post will shift focus a bit from using &lt;code&gt;winapi&lt;&#x2F;code&gt; to possible techniques to perform the various scans.&lt;&#x2F;p&gt;
1455&lt;h2 id=&quot;unknown-initial-value&quot;&gt;Unknown initial value&lt;&#x2F;h2&gt;
1456&lt;details open&gt;&lt;summary&gt;Cheat Engine Tutorial: Step 3&lt;&#x2F;summary&gt;
1457&lt;blockquote&gt;
1458&lt;p&gt;Ok, seeing that you&#x27;ve figured out how to find a value using exact value let&#x27;s move on to the next step.&lt;&#x2F;p&gt;
1459&lt;p&gt;First things first though. Since you are doing a new scan, you have to click on New Scan first, to start a new scan. (You may think this is straighforward, but you&#x27;d be surprised how many people get stuck on that step) I won&#x27;t be explaining this step again, so keep this in mind
1460Now that you&#x27;ve started a new scan, let&#x27;s continue&lt;&#x2F;p&gt;
1461&lt;p&gt;In the previous test we knew the initial value so we could do a exact value, but now we have a status bar where we don&#x27;t know the starting value.
1462We only know that the value is between 0 and 500. And each time you click &#x27;hit me&#x27; you lose some health. The amount you lose each time is shown above the status bar.&lt;&#x2F;p&gt;
1463&lt;p&gt;Again there are several different ways to find the value. (like doing a decreased value by... scan), but I&#x27;ll only explain the easiest. &amp;quot;Unknown initial value&amp;quot;, and decreased value.
1464Because you don&#x27;t know the value it is right now, a exact value wont do any good, so choose as scantype &#x27;Unknown initial value&#x27;, again, the value type is 4-bytes. (most windows apps use 4-bytes)click first scan and wait till it&#x27;s done.&lt;&#x2F;p&gt;
1465&lt;p&gt;When it is done click &#x27;hit me&#x27;. You&#x27;ll lose some of your health. (the amount you lost shows for a few seconds and then disappears, but you don&#x27;t need that)
1466Now go to Cheat Engine, and choose &#x27;Decreased Value&#x27; and click &#x27;Next Scan&#x27;
1467When that scan is done, click hit me again, and repeat the above till you only find a few.&lt;&#x2F;p&gt;
1468&lt;p&gt;We know the value is between 0 and 500, so pick the one that is most likely the address we need, and add it to the list.
1469Now change the health to 5000, to proceed to the next step.&lt;&#x2F;p&gt;
1470&lt;&#x2F;blockquote&gt;
1471&lt;&#x2F;details&gt;
1472&lt;h2 id=&quot;dense-memory-locations&quot;&gt;Dense memory locations&lt;&#x2F;h2&gt;
1473&lt;p&gt;The key thing to notice here is that, when we read memory from another process, we do so over &lt;em&gt;entire regions&lt;&#x2F;em&gt;. A memory region is represented by a starting offset, a size, and a bunch of other things like protection level.&lt;&#x2F;p&gt;
1474&lt;p&gt;When running the first scan for an unknown value, all we need to remember is the starting offset and size for every single region. All the candidate locations that could point to our value fall within this range, so it is enough for us to store the range definition, and not every location within it.&lt;&#x2F;p&gt;
1475&lt;p&gt;To gain a better understanding of what this means, let&#x27;s come up with a more specific scenario. With our current approach of doing things, we store an address (&lt;code&gt;usize&lt;&#x2F;code&gt;) for every location pointing to our desired value. In the case of unknown values, all locations are equally valid, since we don&#x27;t know what value they should point to yet, and any value they point to is good. With this representation, we would end up with a very large vector:&lt;&#x2F;p&gt;
1476&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let locations = vec![0x2000, 0x2001, ..., 0x20ff, 0x2100];
1477&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1478&lt;p&gt;This representation is dense. Every single number in the range &lt;code&gt;0x2000..=0x2100&lt;&#x2F;code&gt; is present. So why bother storing the values individually when the range is enough?:&lt;&#x2F;p&gt;
1479&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let locations = EntireRegion { range: 0x2000..=0x2100 };
1480&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1481&lt;p&gt;Much better! With two &lt;code&gt;usize&lt;&#x2F;code&gt;, one for the starting location and another for the end, we can indicate that we care about all the locations falling in that range.&lt;&#x2F;p&gt;
1482&lt;p&gt;In fact, some accessible memory regions immediately follow eachother, so we could even compact this further and merge regions which are together. But due to their potential differences with regards to protection levels, we will not attempt to merge regions.&lt;&#x2F;p&gt;
1483&lt;p&gt;We don&#x27;t want to get rid of the old way of storing locations, because once we start narrowing them down, we will want to go back to storing just a few candidates. To keep things tidy, let&#x27;s introduce a new &lt;code&gt;enum&lt;&#x2F;code&gt; representing either possibility:&lt;&#x2F;p&gt;
1484&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;use std::ops::Range;
1485
1486pub enum CandidateLocations {
1487    Discrete {
1488        locations: Vec&amp;lt;usize&amp;gt;,
1489    },
1490    Dense {
1491        range: Range&amp;lt;usize&amp;gt;,
1492    }
1493}
1494&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1495&lt;p&gt;Let&#x27;s also introduce another &lt;code&gt;enum&lt;&#x2F;code&gt; to perform the different scan types. For the time being, we will only worry about looking for &lt;code&gt;i32&lt;&#x2F;code&gt; in memory:&lt;&#x2F;p&gt;
1496&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub enum Scan {
1497    Exact(i32),
1498    Unknown,
1499}
1500&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1501&lt;h2 id=&quot;storing-scanned-values&quot;&gt;Storing scanned values&lt;&#x2F;h2&gt;
1502&lt;p&gt;When scanning for exact values, it&#x27;s not necessary to store the value found. We already know they&#x27;re all the same, for example, value &lt;code&gt;42&lt;&#x2F;code&gt;. However, if the value is unknown, we do need to store it so that we can compare it in a subsequent scan to see if the value is the same or it changed. This means the value can be &amp;quot;any within&amp;quot; the read memory chunk:&lt;&#x2F;p&gt;
1503&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub enum Value {
1504    Exact(i32),
1505    AnyWithin(Vec&amp;lt;u8&amp;gt;),
1506}
1507&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1508&lt;p&gt;For every region in memory, there will be some candidate locations and a value (or value range) we need to compare against in subsequent scans:&lt;&#x2F;p&gt;
1509&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub struct Region {
1510    pub info: winapi::um::winnt::MEMORY_BASIC_INFORMATION,
1511    pub locations: CandidateLocations,
1512    pub value: Value,
1513}
1514&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1515&lt;p&gt;With all the data structures needed setup, we can finally refactor our old scanning code into a new method capable of dealing with all these cases. For brevity, I will omit the exact scan, as it remains mostly unchanged:&lt;&#x2F;p&gt;
1516&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;use winapi::um::winnt::MEMORY_BASIC_INFORMATION;
1517
1518...
1519
1520&#x2F;&#x2F; inside `impl Process`
1521pub fn scan_regions(&amp;amp;self, regions: &amp;amp;[MEMORY_BASIC_INFORMATION], scan: Scan) -&amp;gt; Vec&amp;lt;Region&amp;gt; {
1522    regions
1523        .iter()
1524        .flat_map(|region| match scan {
1525            Scan::Exact(n) =&amp;gt; todo!(&amp;quot;old scan implementation&amp;quot;),
1526            Scan::Unknown =&amp;gt; {
1527                let base = region.BaseAddress as usize;
1528                match self.read_memory(region.BaseAddress as _, region.RegionSize) {
1529                    Ok(memory) =&amp;gt; Some(Region {
1530                        info: region.clone(),
1531                        locations: CandidateLocations::Dense {
1532                            range: base..base + region.RegionSize,
1533                        },
1534                        value: Value::AnyWithin(memory),
1535                    }),
1536                    Err(_) =&amp;gt; None,
1537                }
1538            }
1539        })
1540        .collect()
1541}
1542&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1543&lt;p&gt;Time to try it out!&lt;&#x2F;p&gt;
1544&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;impl CandidateLocations {
1545    pub fn len(&amp;amp;self) -&amp;gt; usize {
1546        match self {
1547            CandidateLocations::Discrete { locations } =&amp;gt; locations.len(),
1548            CandidateLocations::Dense { range } =&amp;gt; range.len(),
1549        }
1550    }
1551}
1552
1553...
1554
1555fn main() {
1556    &#x2F;&#x2F; -snip-
1557
1558    println!(&amp;quot;Scanning {} memory regions&amp;quot;, regions.len());
1559    let last_scan = process.scan_regions(&amp;amp;regions, Scan::Unknown);
1560    println!(
1561        &amp;quot;Found {} locations&amp;quot;,
1562        last_scan.iter().map(|r| r.locations.len()).sum::&amp;lt;usize&amp;gt;()
1563    );
1564}
1565&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1566&lt;pre&gt;&lt;code&gt;Scanning 88 memory regions
1567Found 3014656 locations
1568&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1569&lt;p&gt;If we consider misaligned locations, there is a lot of potential addresses where we could look for. Running the same scan on Cheat Engine yields &lt;code&gt;2,449,408&lt;&#x2F;code&gt; addresses, which is pretty close. It&#x27;s probably skipping some additional regions that we are considering. Emulating Cheat Engine to perfection is not a concern for us at the moment, so I&#x27;m not going to investigate what regions it actually uses.&lt;&#x2F;p&gt;
1570&lt;h2 id=&quot;comparing-scanned-values&quot;&gt;Comparing scanned values&lt;&#x2F;h2&gt;
1571&lt;p&gt;Now that we have performed the initial scan and have stored all the &lt;code&gt;CandidateLocations&lt;&#x2F;code&gt; and &lt;code&gt;Value&lt;&#x2F;code&gt;, we can re-implement the &amp;quot;next scan&amp;quot; step to handle any variant of our &lt;code&gt;Scan&lt;&#x2F;code&gt; enum. This enables us to mix-and-match any &lt;code&gt;Scan&lt;&#x2F;code&gt; mode in any order. For example, one could perform an exact scan, then one for decreased values, or start with unknown scan and scan for unchanged values.&lt;&#x2F;p&gt;
1572&lt;p&gt;The tutorial suggests using &amp;quot;decreased value&amp;quot; scan, so let&#x27;s start with that:&lt;&#x2F;p&gt;
1573&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub enum Scan {
1574    Exact(i32),
1575    Unknown,
1576    Decreased, &#x2F;&#x2F; new!
1577}
1578&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1579&lt;p&gt;Other scanning modes, such as decreased by a known amount rather than any decrease, increased, unchanged, changed and so on, are not very different from the &amp;quot;decreased&amp;quot; scan, so I won&#x27;t bore you with the details.&lt;&#x2F;p&gt;
1580&lt;p&gt;I will use a different method to perform a &amp;quot;rescan&amp;quot;, since the first one is a bit more special in that it doesn&#x27;t start with any previous values:&lt;&#x2F;p&gt;
1581&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub fn rescan_regions(&amp;amp;self, regions: &amp;amp;[Region], scan: Scan) -&amp;gt; Vec&amp;lt;Region&amp;gt; {
1582    regions
1583        .iter()
1584        .flat_map(|region| match scan {
1585            Scan::Decreased =&amp;gt; {
1586                let mut locations = Vec::new();
1587                match region.locations {
1588                    CandidateLocations::Dense { range } =&amp;gt; {
1589                        match self.read_memory(range.start, range.end - range.start) {
1590                            Ok(memory) =&amp;gt; match region.value {
1591                                Value::AnyWithin(previous) =&amp;gt; {
1592                                    memory
1593                                        .windows(4)
1594                                        .zip(previous.windows(4))
1595                                        .enumerate()
1596                                        .step_by(4)
1597                                        .for_each(|(offset, (new, old))| {
1598                                            let new = i32::from_ne_bytes([
1599                                                new[0], new[1], new[2], new[3],
1600                                            ]);
1601                                            let old = i32::from_ne_bytes([
1602                                                old[0], old[1], old[2], old[3],
1603                                            ]);
1604                                            if new &amp;lt; old {
1605                                                locations.push(range.start + offset);
1606                                            }
1607                                        });
1608
1609                                    Some(Region {
1610                                        info: region.info.clone(),
1611                                        locations: CandidateLocations::Discrete { locations },
1612                                        value: Value::AnyWithin(memory),
1613                                    })
1614                                }
1615                                _ =&amp;gt; todo!(),
1616                            },
1617                            _ =&amp;gt; todo!(),
1618                        }
1619                    }
1620                    _ =&amp;gt; todo!(),
1621                }
1622            }
1623            _ =&amp;gt; todo!(),
1624        })
1625        .collect()
1626}
1627&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1628&lt;p&gt;If you&#x27;ve skimmed over that, I do not blame you. Here&#x27;s the summary: for every existing region, when executing the scan mode &amp;quot;decreased&amp;quot;, if the previous locations were dense, read the entire memory region. On success, if the previous values were a chunk of memory, iterate over the current and old memory at the same time, and for every aligned &lt;code&gt;i32&lt;&#x2F;code&gt;, if the new value is less, store it.&lt;&#x2F;p&gt;
1629&lt;p&gt;It&#x27;s also making me ill. Before I leave a mess on the floor, does it work?&lt;&#x2F;p&gt;
1630&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;std::thread::sleep(std::time::Duration::from_secs(10));
1631let last_scan = process.rescan_regions(&amp;amp;last_scan, Scan::Decreased);
1632println!(
1633    &amp;quot;Found {} locations&amp;quot;,
1634    last_scan.iter().map(|r| r.locations.len()).sum::&amp;lt;usize&amp;gt;()
1635);
1636&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1637&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;Found 3014656 locations
1638Found 177 locations
1639&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1640&lt;p&gt;Okay, great, let&#x27;s clean up this mess…&lt;&#x2F;p&gt;
1641&lt;h2 id=&quot;refactoring&quot;&gt;Refactoring&lt;&#x2F;h2&gt;
1642&lt;p&gt;Does it also make you uncomfortable to be writing something that you know will end up &lt;em&gt;huge&lt;&#x2F;em&gt; unless you begin refactoring other parts right now? I definitely feel that way. But I think it&#x27;s good discipline to push through with something that works first, even if it&#x27;s nasty, before going on a tangent. Now that we have the basic implementation working, let&#x27;s take on this monster before it eats us alive.&lt;&#x2F;p&gt;
1643&lt;p&gt;First things first, that method is inside an &lt;code&gt;impl&lt;&#x2F;code&gt; block. The deepest nesting level is 13. I almost have to turn around my chair to read the entire thing out!&lt;&#x2F;p&gt;
1644&lt;p&gt;Second, we&#x27;re nesting four matches. Three of them we care about: scan, candidate location, and value. If each of these &lt;code&gt;enum&lt;&#x2F;code&gt; has &lt;code&gt;S&lt;&#x2F;code&gt;, &lt;code&gt;C&lt;&#x2F;code&gt; and &lt;code&gt;V&lt;&#x2F;code&gt; variants respectively, writing each of these by hand will require &lt;code&gt;S * C * V&lt;&#x2F;code&gt; different implementations! Cheat Engine offers 10 different scans, I can think of at least 3 different ways to store candidate locations, and another 3 ways to store the values found. That&#x27;s &lt;code&gt;10 * 3 * 3 = 90&lt;&#x2F;code&gt; different combinations. I am not willing to write out all these&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;, so we need to start introducing some abstractions. Just imagine what a monster function you would end with! The horror!&lt;&#x2F;p&gt;
1645&lt;p&gt;Third, why is the scan being executed in the process? This is something that should be done in the &lt;code&gt;impl Scan&lt;&#x2F;code&gt; instead!&lt;&#x2F;p&gt;
1646&lt;p&gt;Let&#x27;s begin the cleanup:&lt;&#x2F;p&gt;
1647&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub fn rescan_regions(&amp;amp;self, regions: &amp;amp;[Region], scan: Scan) -&amp;gt; Vec&amp;lt;Region&amp;gt; {
1648    todo!()
1649}
1650&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1651&lt;p&gt;I already feel ten times better.&lt;&#x2F;p&gt;
1652&lt;p&gt;Now, this method will unconditionally read the entire memory region, even if the scan or the previous candidate locations don&#x27;t need it&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. In the worst case with a single discrete candidate location, we will be reading a very large chunk of memory when we could have read just the 4 bytes needed for the &lt;code&gt;i32&lt;&#x2F;code&gt;. On the bright side, if there &lt;em&gt;are&lt;&#x2F;em&gt; more locations in this memory region, we will get read of them at the same time&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#4&quot;&gt;4&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. So even if we&#x27;re moving more memory around all the time, it isn&#x27;t &lt;em&gt;too&lt;&#x2F;em&gt; bad.&lt;&#x2F;p&gt;
1653&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;regions
1654    .iter()
1655    .flat_map(
1656        |region| match self.read_memory(region.info.BaseAddress as _, region.info.RegionSize) {
1657            Ok(memory) =&amp;gt; todo!(),
1658            Err(err) =&amp;gt; {
1659                eprintln!(
1660                    &amp;quot;Failed to read {} bytes at {:?}: {}&amp;quot;,
1661                    region.info.RegionSize, region.info.BaseAddress, err,
1662                );
1663                None
1664            }
1665        },
1666    )
1667    .collect()
1668&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1669&lt;p&gt;Great! If reading memory succeeds, we want to rerun the scan:&lt;&#x2F;p&gt;
1670&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;Ok(memory) =&amp;gt; Some(scan.rerun(region, memory)),
1671&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1672&lt;p&gt;The rerun will live inside &lt;code&gt;impl Scan&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
1673&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub fn rerun(&amp;amp;self, region: &amp;amp;Region, memory: Vec&amp;lt;u8&amp;gt;) -&amp;gt; Region {
1674    match self {
1675        Scan::Exact(_) =&amp;gt; self.run(region.info.clone(), memory),
1676        Scan::Unknown =&amp;gt; region.clone(),
1677        Scan::Decreased =&amp;gt; todo!(),
1678    }
1679}
1680&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1681&lt;p&gt;An exact scan doesn&#x27;t care about any previous values, so it behaves like a first scan. The first scan is done by the &lt;code&gt;run&lt;&#x2F;code&gt; function (it contains the implementation factored out of the &lt;code&gt;Process::scan_regions&lt;&#x2F;code&gt; method), which only needs the region information and the current memory chunk we just read.&lt;&#x2F;p&gt;
1682&lt;p&gt;The unknown scan leaves the region unchanged: any value stored is still valid, because it is unknown what we&#x27;re looking for.&lt;&#x2F;p&gt;
1683&lt;p&gt;The decreased scan will have to iterate over all the candidate locations, and compare them with the current memory chunk. But this time, we&#x27;ll abstract this iteration too:&lt;&#x2F;p&gt;
1684&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;impl Region {
1685    fn iter_locations&amp;lt;&#x27;a&amp;gt;(
1686        &amp;amp;&#x27;a self,
1687        new_memory: &amp;amp;&#x27;a [u8],
1688    ) -&amp;gt; impl Iterator&amp;lt;Item = (usize, i32, i32)&amp;gt; + &#x27;a {
1689        match &amp;amp;self.locations {
1690            CandidateLocations::Dense { range } =&amp;gt; range.clone().step_by(4).map(move |addr| {
1691                let old = self.value_at(addr);
1692                let new = i32::from_ne_bytes([
1693                    new_memory[0],
1694                    new_memory[1],
1695                    new_memory[2],
1696                    new_memory[3],
1697                ]);
1698                (addr, old, new)
1699            }),
1700            _ =&amp;gt; todo!(),
1701        }
1702    }
1703}
1704&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1705&lt;p&gt;For a dense candidate location, we iterate over all the 4-aligned addresses (fast scan for &lt;code&gt;i32&lt;&#x2F;code&gt; values), and yield &lt;code&gt;(current address, old value, new value)&lt;&#x2F;code&gt;. This way, the &lt;code&gt;Scan&lt;&#x2F;code&gt; can do anything it wants with the old and new values, and if it finds a match, it can use the address.&lt;&#x2F;p&gt;
1706&lt;p&gt;The &lt;code&gt;value_at&lt;&#x2F;code&gt; method will deal with all the &lt;code&gt;Value&lt;&#x2F;code&gt; variants:&lt;&#x2F;p&gt;
1707&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;fn value_at(&amp;amp;self, addr: usize) -&amp;gt; i32 {
1708    match &amp;amp;self.value {
1709        Value::AnyWithin(chunk) =&amp;gt; {
1710            let base = addr - self.info.BaseAddress as usize;
1711            let bytes = &amp;amp;chunk[base..base + 4];
1712            i32::from_ne_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
1713        }
1714        _ =&amp;gt; todo!(),
1715    }
1716}
1717&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1718&lt;p&gt;This way, &lt;code&gt;iter_locations&lt;&#x2F;code&gt; can easily use any value type. With this, we have all &lt;code&gt;enum&lt;&#x2F;code&gt; covered: &lt;code&gt;Scan&lt;&#x2F;code&gt; in &lt;code&gt;rerun&lt;&#x2F;code&gt;, &lt;code&gt;CandidateLocation&lt;&#x2F;code&gt; in &lt;code&gt;iter_locations&lt;&#x2F;code&gt;, and &lt;code&gt;Value&lt;&#x2F;code&gt; in &lt;code&gt;value_at&lt;&#x2F;code&gt;. Now we can add as many variants as we want, and we will only need to update a single &lt;code&gt;match&lt;&#x2F;code&gt; arm for each of them. Let&#x27;s implement &lt;code&gt;Scan::Decreased&lt;&#x2F;code&gt; and try it out:&lt;&#x2F;p&gt;
1719&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub fn rerun(&amp;amp;self, region: &amp;amp;Region, memory: Vec&amp;lt;u8&amp;gt;) -&amp;gt; Region {
1720    match self {
1721        Scan::Decreased =&amp;gt; Region {
1722            info: region.info.clone(),
1723            locations: CandidateLocations::Discrete {
1724                locations: region
1725                    .iter_locations(&amp;amp;memory)
1726                    .flat_map(|(addr, old, new)| if new &amp;lt; old { Some(addr) } else { None })
1727                    .collect(),
1728            },
1729            value: Value::AnyWithin(memory),
1730        },,
1731    }
1732}
1733&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1734&lt;pre&gt;&lt;code&gt;Found 3014656 locations
1735Found 223791 locations
1736&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1737&lt;p&gt;Hmm… before we went down from &lt;code&gt;3014656&lt;&#x2F;code&gt; to &lt;code&gt;177&lt;&#x2F;code&gt; locations, and now we went down to &lt;code&gt;223791&lt;&#x2F;code&gt;. Where did we go wrong?&lt;&#x2F;p&gt;
1738&lt;p&gt;After spending several hours on this, I can tell you where we went wrong. &lt;code&gt;iter_locations&lt;&#x2F;code&gt; is always accessing the memory range &lt;code&gt;0..4&lt;&#x2F;code&gt;, and not the right address. Here&#x27;s the fix:&lt;&#x2F;p&gt;
1739&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;CandidateLocations::Dense { range } =&amp;gt; range.clone().step_by(4).map(move |addr| {
1740    let old = self.value_at(addr);
1741    let base = addr - self.info.BaseAddress as usize;
1742    let bytes = &amp;amp;new_memory[base..base + 4];
1743    let new = i32::from_ne_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]);
1744    (addr, old, new)
1745}),
1746&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1747&lt;h2 id=&quot;going-beyond&quot;&gt;Going beyond&lt;&#x2F;h2&gt;
1748&lt;p&gt;Let&#x27;s take a look at other possible &lt;code&gt;Scan&lt;&#x2F;code&gt; types. Cheat Engine supports the following initial scan types:&lt;&#x2F;p&gt;
1749&lt;ul&gt;
1750&lt;li&gt;Exact Value&lt;&#x2F;li&gt;
1751&lt;li&gt;Bigger than…&lt;&#x2F;li&gt;
1752&lt;li&gt;Smaller than…&lt;&#x2F;li&gt;
1753&lt;li&gt;Value between…&lt;&#x2F;li&gt;
1754&lt;li&gt;Unknown initial value&lt;&#x2F;li&gt;
1755&lt;&#x2F;ul&gt;
1756&lt;p&gt;&amp;quot;Bigger than&amp;quot; and &amp;quot;Smaller than&amp;quot; can both be represented by &amp;quot;Value between&amp;quot;, so it&#x27;s pretty much just three.&lt;&#x2F;p&gt;
1757&lt;p&gt;For subsequent scans, in addition to the scan types described above, we find:&lt;&#x2F;p&gt;
1758&lt;ul&gt;
1759&lt;li&gt;Increased value&lt;&#x2F;li&gt;
1760&lt;li&gt;Increased value by…&lt;&#x2F;li&gt;
1761&lt;li&gt;Decreased value&lt;&#x2F;li&gt;
1762&lt;li&gt;Decreased value by…&lt;&#x2F;li&gt;
1763&lt;li&gt;Changed value&lt;&#x2F;li&gt;
1764&lt;li&gt;Unchanged value&lt;&#x2F;li&gt;
1765&lt;&#x2F;ul&gt;
1766&lt;p&gt;Not only does Cheat Engine provide all of these scans, but all of them can also be negated. For example, &amp;quot;find values that were not increased by 7&amp;quot;. One could imagine to also support things like &amp;quot;increased value by range&amp;quot;. For the increased and decreased scans, Cheat Engine also supports &amp;quot;at least xx%&amp;quot;, so that if the value changed within the specified percentage interval, it will be considered.&lt;&#x2F;p&gt;
1767&lt;p&gt;What about &lt;code&gt;CandidateLocations&lt;&#x2F;code&gt;? I can&#x27;t tell you how Cheat Engine stores these, but I can tell you that &lt;code&gt;CandidateLocations::Discrete&lt;&#x2F;code&gt; can still be quite inefficient. Imagine you&#x27;ve started with a scan for unknown values and then ran a scan for unchanged valueus. Most values in memory will have been unchanged, but with our current implementation, we are now storing an entire &lt;code&gt;usize&lt;&#x2F;code&gt; address for each of these. One option would be to introduce &lt;code&gt;CandidateLocations::Sparse&lt;&#x2F;code&gt;, which would be a middle ground. You could implement it like &lt;code&gt;Dense&lt;&#x2F;code&gt; and include a vector of booleans telling you which values to consider, or go smaller and use a bitstring or bit vector. You could use a sparse vector data structure.&lt;&#x2F;p&gt;
1768&lt;p&gt;&lt;code&gt;Value&lt;&#x2F;code&gt; is very much like &lt;code&gt;CandidateLocations&lt;&#x2F;code&gt;, except that it stores a value to compare against and not an address. Here we can either have an exact value, or an older copy of the memory. Again, keeping a copy of the entire memory chunk when all we need is a handful of values is inefficient. You could keep a mapping from addresses to values if you don&#x27;t have too many. Or you could shrink and fragment the copied memory in a more optimal way. There&#x27;s a lot of room for improvement!&lt;&#x2F;p&gt;
1769&lt;p&gt;What if, despite all of the efforts above, we still don&#x27;t have enough RAM to store all this information? The Cheat Engine Tutorial doesn&#x27;t use a lot of memory, but as soon as you try scanning bigger programs, like games, you may find yourself needing several gigabytes worth of memory to remember all the found values in order to compare them in subsequent scans. You may even need to consider dumping all the regions to a file and read from it to run the comparisons. For example, running a scan for &amp;quot;unknown value&amp;quot; in Cheat Engine brings its memory up by the same amount of memory used by the process scanned (which makes sense), but as soon as I ran a scan for &amp;quot;unchanged value&amp;quot; over the misaligned values, Cheat Engine&#x27;s disk usage skyrocketed to 1GB&#x2F;s (!) for several seconds on my SSD. After it finished, memory usage went down to normal. It was very likely writing out all candidate locations to disk.&lt;&#x2F;p&gt;
1770&lt;h2 id=&quot;finale&quot;&gt;Finale&lt;&#x2F;h2&gt;
1771&lt;p&gt;There is a lot of things to learn from Cheat Engine just by observing its behaviour, and we&#x27;re only scratching its surface.&lt;&#x2F;p&gt;
1772&lt;p&gt;In the &lt;a href=&quot;&#x2F;blog&#x2F;woce-4&quot;&gt;next post&lt;&#x2F;a&gt;, we&#x27;ll tackle the fourth step of the tutorial: Floating points. So far, we have only been working with &lt;code&gt;i32&lt;&#x2F;code&gt; for simplicity. We will need to update our code to be able to account for different data types, which will make it easy to support other types like &lt;code&gt;i16&lt;&#x2F;code&gt;, &lt;code&gt;i64&lt;&#x2F;code&gt;, or even strings, represented as an arbitrary sequence of bytes.&lt;&#x2F;p&gt;
1773&lt;p&gt;As usual, you can &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lonami&#x2F;memo&quot;&gt;obtain the code for this post&lt;&#x2F;a&gt; over at my GitHub. You can run &lt;code&gt;git checkout step3&lt;&#x2F;code&gt; after cloning the repository to get the right version of the code. This version is a bit cleaner than the one presented in the blog, and contains some of the things described in the &lt;a href=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;woce-3&#x2F;#going-beyond&quot;&gt;Going beyond&lt;&#x2F;a&gt; section. Until next time!&lt;&#x2F;p&gt;
1774&lt;h3 id=&quot;footnotes&quot;&gt;Footnotes&lt;&#x2F;h3&gt;
1775&lt;div class=&quot;footnote-definition&quot; id=&quot;1&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;&#x2F;sup&gt;
1776&lt;p&gt;Well, technically, we will perform a million memory reads&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#5&quot;&gt;5&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. The issue here is the million calls to &lt;code&gt;ReadProcessMemory&lt;&#x2F;code&gt;, not reading memory per se.&lt;&#x2F;p&gt;
1777&lt;&#x2F;div&gt;
1778&lt;div class=&quot;footnote-definition&quot; id=&quot;2&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;2&lt;&#x2F;sup&gt;
1779&lt;p&gt;Not currently. After a basic implementation works, writing each implementation by hand and fine-tuning them by treating each of them as a special case could yield significant speed improvements. So although it would be a lot of work, this option shouldn&#x27;t be ruled out completely.&lt;&#x2F;p&gt;
1780&lt;&#x2F;div&gt;
1781&lt;div class=&quot;footnote-definition&quot; id=&quot;3&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;3&lt;&#x2F;sup&gt;
1782&lt;p&gt;You could ask the candidate locations where one should read, which would still keep the code reasonably simple.&lt;&#x2F;p&gt;
1783&lt;&#x2F;div&gt;
1784&lt;div class=&quot;footnote-definition&quot; id=&quot;4&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;4&lt;&#x2F;sup&gt;
1785&lt;p&gt;You could also optimize for this case by determining both the smallest and largest address, and reading enough to cover them both. Or apply additional heuristics to only do so if the ratio of the size you&#x27;re reading compared to the size you need isn&#x27;t too large and abort the joint read otherwise. There is a lot of room for optimization here.&lt;&#x2F;p&gt;
1786&lt;&#x2F;div&gt;
1787&lt;div class=&quot;footnote-definition&quot; id=&quot;5&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;5&lt;&#x2F;sup&gt;
1788&lt;p&gt;(A footnote in a footnote?) The machine registers, memory cache and compiler will all help lower this cost, so the generated executable might not actually need that many reads from RAM. But that&#x27;s getting way too deep into the details now.&lt;&#x2F;p&gt;
1789&lt;&#x2F;div&gt;
1790</content>
1791	</entry>
1792	<entry xml:lang="en">
1793		<title>Writing our own Cheat Engine: Exact Value scanning</title>
1794		<published>2021-02-12T00:00:00+00:00</published>
1795		<updated>2021-02-19T00:00:00+00:00</updated>
1796		<link href="https://lonami.dev/blog/woce-2/" type="text/html"/>
1797		<id>https://lonami.dev/blog/woce-2/</id>
1798		<content type="html">&lt;p&gt;This is part 2 on the &lt;em&gt;Writing our own Cheat Engine&lt;&#x2F;em&gt; series:&lt;&#x2F;p&gt;
1799&lt;ul&gt;
1800&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-1&quot;&gt;Part 1: Introduction&lt;&#x2F;a&gt; (start here if you&#x27;re new to the series!)&lt;&#x2F;li&gt;
1801&lt;li&gt;Part 2: Exact Value scanning&lt;&#x2F;li&gt;
1802&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-3&quot;&gt;Part 3: Unknown initial value&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
1803&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-4&quot;&gt;Part 4: Floating points&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
1804&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-5&quot;&gt;Part 5: Code finder&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
1805&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-6&quot;&gt;Part 6: Pointers&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
1806&lt;&#x2F;ul&gt;
1807&lt;p&gt;In the introduction, we spent a good deal of time enumerating all running processes just so we could find out the pid we cared about. With the pid now in our hands, we can do pretty much anything to its corresponding process.&lt;&#x2F;p&gt;
1808&lt;p&gt;It&#x27;s now time to read the process&#x27; memory and write to it. If our process was a single-player game, this would enable us to do things like setting a very high value on the player&#x27;s current health pool, making us invincible. This technique will often not work for multi-player games, because the server likely knows your true current health (the most you could probably do is make the client render an incorrect value). However, if the server is crappy and it trusts the client, then you&#x27;re still free to mess around with your current health.&lt;&#x2F;p&gt;
1809&lt;p&gt;Even if we don&#x27;t want to write to the process&#x27; memory, reading is still very useful. Maybe you could enhance your experience by making a custom overlay that displays useful information, or something that makes noise if it detects the life is too low, or even simulating a keyboard event to automatically recover some mana when you&#x27;re running low.&lt;&#x2F;p&gt;
1810&lt;p&gt;Be warned about anti-cheat systems. Anything beyond a basic game is likely to have some protection measures in place, making the analysis more difficult (perhaps the values are scrambled in memory), or even pinging the server if it detects something fishy.&lt;&#x2F;p&gt;
1811&lt;p&gt;&lt;strong&gt;I am not responsible for any bans!&lt;&#x2F;strong&gt; Use your brain before messing with online games, and don&#x27;t ruin the fun for everyone else. If you get caught for cheating, I don&#x27;t want to know about it.&lt;&#x2F;p&gt;
1812&lt;p&gt;Now that all &lt;a href=&quot;https:&#x2F;&#x2F;www.urbandictionary.com&#x2F;define.php?term=script%20kiddie&quot;&gt;script kiddies&lt;&#x2F;a&gt; have left the room, let&#x27;s proceed with the post.&lt;&#x2F;p&gt;
1813&lt;h2 id=&quot;exact-value-scanning&quot;&gt;Exact Value scanning&lt;&#x2F;h2&gt;
1814&lt;details open&gt;&lt;summary&gt;Cheat Engine Tutorial: Step 2&lt;&#x2F;summary&gt;
1815&lt;blockquote&gt;
1816&lt;p&gt;Now that you have opened the tutorial with Cheat Engine let&#x27;s get on with the next step.&lt;&#x2F;p&gt;
1817&lt;p&gt;You can see at the bottom of this window is the text Health: xxx. Each time you click &#x27;Hit me&#x27; your health gets decreased.&lt;&#x2F;p&gt;
1818&lt;p&gt;To get to the next step you have to find this value and change it to 1000&lt;&#x2F;p&gt;
1819&lt;p&gt;To find the value there are different ways, but I&#x27;ll tell you about the easiest, &#x27;Exact Value&#x27;: First make sure value type is set to at least 2-bytes or 4-bytes. 1-byte will also work, but you&#x27;ll run into an easy to fix problem when you&#x27;ve found the address and want to change it. The 8-byte may perhaps works if the bytes after the address are 0, but I wouldn&#x27;t take the bet. Single, double, and the other scans just don&#x27;t work, because they store the value in a different way.&lt;&#x2F;p&gt;
1820&lt;p&gt;When the value type is set correctly, make sure the scantype is set to &#x27;Exact Value&#x27;. Then fill in the number your health is in the value box. And click &#x27;First Scan&#x27;. After a while (if you have a extremely slow pc) the scan is done and the results are shown in the list on the left&lt;&#x2F;p&gt;
1821&lt;p&gt;If you find more than 1 address and you don&#x27;t know for sure which address it is, click &#x27;Hit me&#x27;, fill in the new health value into the value box, and click &#x27;Next Scan&#x27;. Repeat this until you&#x27;re sure you&#x27;ve found it. (that includes that there&#x27;s only 1 address in the list.....)&lt;&#x2F;p&gt;
1822&lt;p&gt;Now double click the address in the list on the left. This makes the address pop-up in the list at the bottom, showing you the current value. Double click the value, (or select it and press enter), and change the value to 1000.&lt;&#x2F;p&gt;
1823&lt;p&gt;If everything went ok the next button should become enabled, and you&#x27;re ready for the next step.&lt;&#x2F;p&gt;
1824&lt;p&gt;Note: If you did anything wrong while scanning, click &amp;quot;New Scan&amp;quot; and repeat the scanning again. Also, try playing around with the value and click &#x27;hit me&#x27;&lt;&#x2F;p&gt;
1825&lt;&#x2F;blockquote&gt;
1826&lt;&#x2F;details&gt;
1827&lt;h2 id=&quot;our-first-scan&quot;&gt;Our First Scan&lt;&#x2F;h2&gt;
1828&lt;p&gt;The Cheat Engine tutorial talks about &amp;quot;value types&amp;quot; and &amp;quot;scan types&amp;quot; like &amp;quot;exact value&amp;quot;.&lt;&#x2F;p&gt;
1829&lt;p&gt;The &lt;strong&gt;value types&lt;&#x2F;strong&gt; will help us narrow down &lt;em&gt;what&lt;&#x2F;em&gt; we&#x27;re looking for. For example, the integer type &lt;code&gt;i32&lt;&#x2F;code&gt; is represented in memory as 32 bits, or 4 bytes. However, &lt;code&gt;f32&lt;&#x2F;code&gt; is &lt;em&gt;also&lt;&#x2F;em&gt; represented by 4 bytes, and so is &lt;code&gt;u32&lt;&#x2F;code&gt;. Or perhaps the 4 bytes represent RGBA values of a color! So any 4 bytes in memory can be interpreted in many ways, and it&#x27;s up to us to decide which way we interpret the bytes in.&lt;&#x2F;p&gt;
1830&lt;p&gt;When programming, numbers which are 32-bit wide are common, as they&#x27;re a good (and fast) size to work with. Scanning for this type is often a good bet. For positive numbers, &lt;code&gt;i32&lt;&#x2F;code&gt; is represented the same as &lt;code&gt;u32&lt;&#x2F;code&gt; in memory, so even if the value turns out to not be signed, the scan is likely to work. Focusing on &lt;code&gt;i32&lt;&#x2F;code&gt; will save us from scanning for &lt;code&gt;f32&lt;&#x2F;code&gt; or even other types, like interpreting 8 bytes for &lt;code&gt;i64&lt;&#x2F;code&gt;, &lt;code&gt;f64&lt;&#x2F;code&gt;, or less bytes like &lt;code&gt;i16&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
1831&lt;p&gt;The &lt;strong&gt;scan types&lt;&#x2F;strong&gt; will help us narrow down &lt;em&gt;how&lt;&#x2F;em&gt; we&#x27;re looking for a value. Scanning for an exact value means what you think it does: interpret all 4 bytes in the process&#x27; memory as our value type, and check if they exactly match our value. This will often yield a lot of candidates, but it will be enough to get us started. Variations of the exact scan include checking for all values below a threshold, above, in between, or even just… unknown.&lt;&#x2F;p&gt;
1832&lt;p&gt;What&#x27;s the point of scanning for unknown values if &lt;em&gt;everything&lt;&#x2F;em&gt; in memory is unknown? Sometimes you don&#x27;t have a concrete value. Maybe your health pool is a bar and it nevers tell you how much health you actually have, just a visual indicator of your percentage left, even if the health is not stored as a percentage. As we will find later on, scanning for unknown values is more useful than it might appear at first.&lt;&#x2F;p&gt;
1833&lt;p&gt;We can access the memory of our own program by guessing random pointers and trying to read from them. But Windows isolates the memory of each program, so no pointer we could ever guess will let us read from the memory of another process. Luckily for us, searching for &amp;quot;read process memory winapi&amp;quot; leads us to the &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;memoryapi&#x2F;nf-memoryapi-readprocessmemory&quot;&gt;&lt;code&gt;ReadProcessMemory&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; function. Spot on.&lt;&#x2F;p&gt;
1834&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub fn read_memory(&amp;amp;self, addr: usize, n: usize) -&amp;gt; io::Result&amp;lt;Vec&amp;lt;u8&amp;gt;&amp;gt; {
1835    todo!()
1836}
1837&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1838&lt;p&gt;Much like trying to dereference a pointer pointing to released memory or even null, reading from an arbitrary address can fail for the same reasons (and more). We will want to signal this with &lt;code&gt;io::Result&lt;&#x2F;code&gt;. It&#x27;s funny to note that, even though we&#x27;re doing something that seems wildly unsafe (reading arbitrary memory, even if the other process is mutating it at the same time), the function is perfectly safe. If we cannot read something, it will return &lt;code&gt;Err&lt;&#x2F;code&gt;, but if it succeeds, it has taken a snapshot of the memory of the process, and the returned value will be correctly initialized.&lt;&#x2F;p&gt;
1839&lt;p&gt;The function will be defined inside our &lt;code&gt;impl Process&lt;&#x2F;code&gt;, since it conveniently holds an open handle to the process in question. It takes &lt;code&gt;&amp;amp;self&lt;&#x2F;code&gt;, because we do not need to mutate anything in the &lt;code&gt;Process&lt;&#x2F;code&gt; instance. After adding the &lt;code&gt;memoryapi&lt;&#x2F;code&gt; feature to &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;, we can perform the call:&lt;&#x2F;p&gt;
1840&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let mut buffer = Vec::&amp;lt;u8&amp;gt;::with_capacity(n);
1841let mut read = 0;
1842
1843&#x2F;&#x2F; SAFETY: the buffer points to valid memory, and the buffer size is correctly set.
1844if unsafe {
1845    winapi::um::memoryapi::ReadProcessMemory(
1846        self.handle.as_ptr(),
1847        addr as *const _,
1848        buffer.as_mut_ptr().cast(),
1849        buffer.capacity(),
1850        &amp;amp;mut read,
1851    )
1852} == FALSE
1853{
1854    Err(io::Error::last_os_error())
1855} else {
1856    &#x2F;&#x2F; SAFETY: the call succeeded and `read` contains the amount of bytes written.
1857    unsafe { buffer.set_len(read as usize) };
1858    Ok(buffer)
1859}
1860&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1861&lt;p&gt;Great! But the address space is somewhat large. 64 bits large. Eighteen quintillion, four hundred and forty-six quadrillion, seven hundred and forty-four trillion, seventy-three billion, seven hundred and nine million, five hundred and fifty-one thousand, six hundred and sixteen&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; large. You gave up reading that, didn&#x27;t you? Anyway, 18&#x27;446&#x27;744&#x27;073&#x27;709&#x27;551&#x27;616 is a &lt;em&gt;big&lt;&#x2F;em&gt; number.&lt;&#x2F;p&gt;
1862&lt;p&gt;I am not willing to wait for the program to scan over so many values. I don&#x27;t even have 16 &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Orders_of_magnitude_(data)&quot;&gt;exbibytes&lt;&#x2F;a&gt; of RAM installed on my laptop yet&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;! What&#x27;s up with that?&lt;&#x2F;p&gt;
1863&lt;h2 id=&quot;memory-regions&quot;&gt;Memory regions&lt;&#x2F;h2&gt;
1864&lt;p&gt;The program does not actually have all that memory allocated (surprise!). Random-guessing an address is extremely likely to point out to invalid memory. Reading from the start of the address space all the way to the end would not be any better. And we &lt;strong&gt;need&lt;&#x2F;strong&gt; to do better.&lt;&#x2F;p&gt;
1865&lt;p&gt;We need to query for the memory regions allocated to the program. For this purpose we can use &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;memoryapi&#x2F;nf-memoryapi-virtualqueryex&quot;&gt;&lt;code&gt;VirtualQueryEx&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
1866&lt;blockquote&gt;
1867&lt;p&gt;Retrieves information about a range of pages within the virtual address space of a specified process.&lt;&#x2F;p&gt;
1868&lt;&#x2F;blockquote&gt;
1869&lt;p&gt;We have enumerated things before, and this function is not all that different.&lt;&#x2F;p&gt;
1870&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;fn memory_regions(&amp;amp;self) -&amp;gt; io::Result&amp;lt;winapi::um::winnt::MEMORY_BASIC_INFORMATION&amp;gt; {
1871    let mut info = MaybeUninit::uninit();
1872
1873    &#x2F;&#x2F; SAFETY: the info structure points to valid memory.
1874    let written = unsafe {
1875        winapi::um::memoryapi::VirtualQueryEx(
1876            self.handle.as_ptr(),
1877            std::ptr::null(),
1878            info.as_mut_ptr(),
1879            mem::size_of::&amp;lt;winapi::um::winnt::MEMORY_BASIC_INFORMATION&amp;gt;(),
1880        )
1881    };
1882    if written == 0 {
1883        Err(io::Error::last_os_error())
1884    } else {
1885        &#x2F;&#x2F; SAFETY: a non-zero amount was written to the structure
1886        Ok(unsafe { info.assume_init() })
1887    }
1888}
1889&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1890&lt;p&gt;We start with a base address of zero&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; (&lt;code&gt;std::ptr::null()&lt;&#x2F;code&gt;), and ask the function to tell us what&#x27;s in there. Let&#x27;s try it out, with the &lt;code&gt;impl-debug&lt;&#x2F;code&gt; crate feature in &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
1891&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;dbg!(process.memory_regions());
1892&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1893&lt;pre&gt;&lt;code&gt;&amp;gt;cargo run
1894Compiling memo v0.1.0
1895
1896error[E0277]: `winapi::um::winnt::MEMORY_BASIC_INFORMATION` doesn&#x27;t implement `std::fmt::Debug`
1897   --&amp;gt; src\main.rs:185:5
1898    |
1899185 |     dbg!(process.memory_regions());
1900    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `winapi::um::winnt::MEMORY_BASIC_INFORMATION` cannot be formatted using `{:?}` because it doesn&#x27;t implement `std::fmt::Debug`
1901&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1902&lt;p&gt;That&#x27;s annoying. It seems not everything has an &lt;code&gt;impl std::fmt::Debug&lt;&#x2F;code&gt;, and &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;retep998&#x2F;winapi-rs&#x2F;issues&#x2F;548#issuecomment-355278090&quot;&gt;you&#x27;re supposed to send a PR&lt;&#x2F;a&gt; if you want it to have debug, even if the &lt;code&gt;impl-debug&lt;&#x2F;code&gt; feature is set. I&#x27;m surprised they don&#x27;t auto-generate all of this and have to rely on manually adding &lt;code&gt;Debug&lt;&#x2F;code&gt; as needed? Oh well, let&#x27;s get rid of the feature and print it out ourselves:&lt;&#x2F;p&gt;
1903&lt;pre&gt;&lt;code&gt;eprintln!(
1904    &amp;quot;Region:
1905    BaseAddress: {:?}
1906    AllocationBase: {:?}
1907    AllocationProtect: {:?}
1908    RegionSize: {:?}
1909    State: {:?}
1910    Protect: {:?}
1911    Type: {:?}&amp;quot;,
1912    region.BaseAddress,
1913    region.AllocationBase,
1914    region.AllocationProtect,
1915    region.RegionSize,
1916    region.State,
1917    region.Protect,
1918    region.Type,
1919);
1920&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1921&lt;p&gt;Hopefully we don&#x27;t need to do this often:&lt;&#x2F;p&gt;
1922&lt;pre&gt;&lt;code&gt;&amp;gt;cargo run
1923   Compiling memo v0.1.0
1924    Finished dev [unoptimized + debuginfo] target(s) in 0.60s
1925     Running `target\debug\memo.exe`
1926
1927Region:
1928    BaseAddress: 0x0
1929    AllocationBase: 0x0
1930    AllocationProtect: 0
1931    RegionSize: 65536
1932    State: 65536
1933    Protect: 1
1934    Type: 0
1935&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1936&lt;p&gt;Awesome! There is a region at &lt;code&gt;null&lt;&#x2F;code&gt;, and the &lt;code&gt;AllocationProtect&lt;&#x2F;code&gt; of zero indicates that &amp;quot;the caller does not have access&amp;quot; when the region was created. However, &lt;code&gt;Protect&lt;&#x2F;code&gt; is &lt;code&gt;1&lt;&#x2F;code&gt;, and that is the &lt;em&gt;current&lt;&#x2F;em&gt; protection level. A value of one indicates &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;memory&#x2F;memory-protection-constants&quot;&gt;&lt;code&gt;PAGE_NOACCESS&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
1937&lt;blockquote&gt;
1938&lt;p&gt;Disables all access to the committed region of pages. An attempt to read from, write to, or execute the committed region results in an access violation.&lt;&#x2F;p&gt;
1939&lt;&#x2F;blockquote&gt;
1940&lt;p&gt;Now that we know that the first region starts at 0 and has a size of 64 KiB, we can simply query for the page at &lt;code&gt;(current base + current size)&lt;&#x2F;code&gt; to fetch the next region. Essentially, we want to loop until it fails, after which we&#x27;ll know there are no more pages&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#4&quot;&gt;4&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;:&lt;&#x2F;p&gt;
1941&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub fn memory_regions(&amp;amp;self) -&amp;gt; Vec&amp;lt;winapi::um::winnt::MEMORY_BASIC_INFORMATION&amp;gt; {
1942    let mut base = 0;
1943    let mut regions = Vec::new();
1944    let mut info = MaybeUninit::uninit();
1945
1946    loop {
1947        &#x2F;&#x2F; SAFETY: the info structure points to valid memory.
1948        let written = unsafe {
1949            winapi::um::memoryapi::VirtualQueryEx(
1950                self.handle.as_ptr(),
1951                base as *const _,
1952                info.as_mut_ptr(),
1953                mem::size_of::&amp;lt;winapi::um::winnt::MEMORY_BASIC_INFORMATION&amp;gt;(),
1954            )
1955        };
1956        if written == 0 {
1957            break regions;
1958        }
1959        &#x2F;&#x2F; SAFETY: a non-zero amount was written to the structure
1960        let info = unsafe { info.assume_init() };
1961        base = info.BaseAddress as usize + info.RegionSize;
1962        regions.push(info);
1963    }
1964}
1965&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1966&lt;p&gt;&lt;code&gt;RegionSize&lt;&#x2F;code&gt; is:&lt;&#x2F;p&gt;
1967&lt;blockquote&gt;
1968&lt;p&gt;The size of the region beginning at the base address in which all pages have identical attributes, in bytes.&lt;&#x2F;p&gt;
1969&lt;&#x2F;blockquote&gt;
1970&lt;p&gt;…which also hints that the value we want is &amp;quot;base address&amp;quot;, not the &amp;quot;allocation base&amp;quot;. With these two values, we can essentially iterate over all the page ranges:&lt;&#x2F;p&gt;
1971&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;dbg!(process.memory_regions().len());
1972&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1973&lt;pre&gt;&lt;code&gt;&amp;gt;cargo run
1974   Compiling memo v0.1.0
1975    Finished dev [unoptimized + debuginfo] target(s) in 0.63s
1976     Running `target\debug\memo.exe`
1977
1978[src\main.rs:189] process.memory_regions().len() = 367
1979&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1980&lt;p&gt;That&#x27;s a lot of pages!&lt;&#x2F;p&gt;
1981&lt;h2 id=&quot;protection-levels&quot;&gt;Protection levels&lt;&#x2F;h2&gt;
1982&lt;p&gt;Let&#x27;s try to narrow the amount of pages down. How many pages aren&#x27;t &lt;code&gt;PAGE_NOACCESS&lt;&#x2F;code&gt;?&lt;&#x2F;p&gt;
1983&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;dbg!(process
1984    .memory_regions()
1985    .into_iter()
1986    .filter(|p| p.Protect != winapi::um::winnt::PAGE_NOACCESS)
1987    .count());
1988&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1989&lt;pre&gt;&lt;code&gt;295
1990&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1991&lt;p&gt;Still a fair bit! Most likely, there are just a few interleaved &lt;code&gt;NOACCESS&lt;&#x2F;code&gt; pages, and the rest are allocated each with different protection levels. How much memory do we need to scan through?&lt;&#x2F;p&gt;
1992&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;dbg!(process
1993    .memory_regions()
1994    .into_iter()
1995    .filter(|p| p.Protect != winapi::um::winnt::PAGE_NOACCESS)
1996    .map(|p| p.RegionSize)
1997    .sum::&amp;lt;usize&amp;gt;());
1998&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
1999&lt;pre&gt;&lt;code&gt;4480434176
2000&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2001&lt;p&gt;Wait, what? What do you mean over 4 GiB? The Task Manager claims that the Cheat Engine Tutorial is only using 2.1 MB worth of RAM! Perhaps we can narrow down the &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;memory&#x2F;memory-protection-constants&quot;&gt;protection levels&lt;&#x2F;a&gt; a bit more. If you look at the scan options in Cheat Engine, you will notice the &amp;quot;Memory Scan Options&amp;quot; groupbox. By default, it only scans for memory that is writable, and doesn&#x27;t care if it&#x27;s executable or not:&lt;&#x2F;p&gt;
2002&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let mask = winnt::PAGE_EXECUTE_READWRITE
2003    | winnt::PAGE_EXECUTE_WRITECOPY
2004    | winnt::PAGE_READWRITE
2005    | winnt::PAGE_WRITECOPY;
2006
2007dbg!(process
2008    .memory_regions()
2009    .into_iter()
2010    .filter(|p| (p.Protect &amp;amp; mask) != 0)
2011    .map(|p| p.RegionSize)
2012    .sum::&amp;lt;usize&amp;gt;());
2013&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2014&lt;p&gt;Each memory protection level has its own bit, so we can OR them all together to have a single mask. When ANDing this mask with the protection level, if any bit is set, it will be non-zero, meaning we want to keep this region.&lt;&#x2F;p&gt;
2015&lt;p&gt;Don&#x27;t ask me why there isn&#x27;t a specific bit for &amp;quot;write&amp;quot;, &amp;quot;read&amp;quot;, &amp;quot;execute&amp;quot;, and there are only bits for combinations. I guess this way Windows forbids certain combinations.&lt;&#x2F;p&gt;
2016&lt;pre&gt;&lt;code&gt;2580480
2017&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2018&lt;p&gt;Hey, that&#x27;s close to the value shown by the Task Manager! A handfull of megabytes is a lot more manageable than 4 entire gigabytes.&lt;&#x2F;p&gt;
2019&lt;h2 id=&quot;actually-running-our-first-scan&quot;&gt;Actually running our First Scan&lt;&#x2F;h2&gt;
2020&lt;p&gt;Okay, we have all the memory regions from which the program can read, write, or execute. Now we also can read the memory in these regions:&lt;&#x2F;p&gt;
2021&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let regions = process
2022    .memory_regions()
2023    .into_iter()
2024    .filter(|p| (p.Protect &amp;amp; mask) != 0)
2025    .collect::&amp;lt;Vec&amp;lt;_&amp;gt;&amp;gt;();
2026
2027println!(&amp;quot;Scanning {} memory regions&amp;quot;, regions.len());
2028
2029regions.into_iter().for_each(|region| {
2030    match process.read_memory(region.BaseAddress as _, region.RegionSize) {
2031        Ok(memory) =&amp;gt; todo!(),
2032        Err(err) =&amp;gt; eprintln!(
2033            &amp;quot;Failed to read {} bytes at {:?}: {}&amp;quot;,
2034            region.RegionSize, region.BaseAddress, err,
2035        ),
2036    }
2037})
2038&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2039&lt;p&gt;All that&#x27;s left is for us to scan for a target value. To do this, we want to iterate over all the &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;std&#x2F;primitive.slice.html#method.windows&quot;&gt;&lt;code&gt;slice::windows&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; of size equal to the size of our scan type.&lt;&#x2F;p&gt;
2040&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let target: i32 = ...;
2041let target = target.to_ne_bytes();
2042
2043&#x2F;&#x2F; -snip-
2044
2045&#x2F;&#x2F; inside the Ok match, replacing the todo!() -- this is where the first scan happens
2046Ok(memory) =&amp;gt; memory
2047    .windows(target.len())
2048    .enumerate()
2049    .for_each(|(offset, window)| {
2050        if window == target {
2051            println!(
2052                &amp;quot;Found exact value at [{:?}+{:x}]&amp;quot;,
2053                region.BaseAddress, offset
2054            );
2055        }
2056    })
2057&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2058&lt;p&gt;We convert the 32-bit exact target value to its memory representation as a byte array in &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;std&#x2F;primitive.i32.html#method.to_ne_bytes&quot;&gt;native byte order&lt;&#x2F;a&gt;. This way we can compare the target bytes with the window bytes. Another option is to interpret the window bytes as an &lt;code&gt;i32&lt;&#x2F;code&gt; with &lt;code&gt;from_be_bytes&lt;&#x2F;code&gt;, but &lt;code&gt;slice::windows&lt;&#x2F;code&gt; gives us slices of type &lt;code&gt;&amp;amp;[u8]&lt;&#x2F;code&gt;, and &lt;code&gt;from_be_bytes&lt;&#x2F;code&gt; wants an &lt;code&gt;[u8; 4]&lt;&#x2F;code&gt; array, so it&#x27;s a bit more annoying to convert.&lt;&#x2F;p&gt;
2059&lt;p&gt;This is enough to find the value in the process&#x27; memory!&lt;&#x2F;p&gt;
2060&lt;pre&gt;&lt;code&gt;Found exact value at [0x10000+aec]
2061Failed to read 12288 bytes at 0x13f8000: Only part of a ReadProcessMemory or WriteProcessMemory request was completed. (os error 299)
2062Found exact value at [0x14f0000+3188]
2063Found exact value at [0x14f0000+ac74]
2064...
2065Found exact value at [0x10030e000+1816]
2066Found exact value at [0x7ff8f7b93000+441a]
2067...
2068Found exact value at [0x7ff8fb381000+4023]
2069&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2070&lt;p&gt;The tutorial starts out with health &amp;quot;100&amp;quot;, which is what I scanned. Apparently, there are nearly a hundred of &lt;code&gt;100&lt;&#x2F;code&gt;-valued integers stored in the memory of the tutorial.&lt;&#x2F;p&gt;
2071&lt;p&gt;Attentive readers will notice that some values are located at an offset modulo 4. In Cheat Engine, this is known as &amp;quot;Fast Scan&amp;quot;, which is enabled by default with an alignment of 4. Most of the time, values are aligned in memory, and this alignment often corresponds with the size of the type itself. For 4-byte integers, it&#x27;s common that they&#x27;re 4-byte aligned.&lt;&#x2F;p&gt;
2072&lt;p&gt;We can perform a fast scan ourselves with &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;std&#x2F;iter&#x2F;trait.Iterator.html#method.step_by&quot;&gt;&lt;code&gt;step_by&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#5&quot;&gt;5&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;:&lt;&#x2F;p&gt;
2073&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;memory
2074    .windows(target.len())
2075    .enumerate()
2076    .step_by(4)
2077    .for_each(...)
2078&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2079&lt;p&gt;As a bonus, over half the addresses are gone, so we have less results to worry about&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#6&quot;&gt;6&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;.&lt;&#x2F;p&gt;
2080&lt;h2 id=&quot;next-scan&quot;&gt;Next Scan&lt;&#x2F;h2&gt;
2081&lt;p&gt;The first scan gave us way too many results. We have no way to tell which is the correct one, as they all have the same value. What we need to do is a &lt;em&gt;second&lt;&#x2F;em&gt; scan at the &lt;em&gt;locations we just found&lt;&#x2F;em&gt;. This way, we can get a second reading, and compare it against a new value. If it&#x27;s the same, we&#x27;re on good track, and if not, we can discard a location. Repeating this process lets us cut the hundreds of potential addresses to just a handful of them.&lt;&#x2F;p&gt;
2082&lt;p&gt;For example, let&#x27;s say we&#x27;re scanning our current health of &lt;code&gt;100&lt;&#x2F;code&gt; in a game. This gives us over a hundred addresses that point to the value of &lt;code&gt;100&lt;&#x2F;code&gt;. If we go in-game and get hit&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#7&quot;&gt;7&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; by some enemy and get our health down to, say, &lt;code&gt;99&lt;&#x2F;code&gt; (we have a lot of defense), we can then read the memory at the hundred memory locations we found before. If this second reading is not &lt;code&gt;99&lt;&#x2F;code&gt;, we know the address does not actually point to our health pool and it just happened to also contain a &lt;code&gt;100&lt;&#x2F;code&gt; on the first scan. This address can be removed from the list of potential addresses pointing to our health.&lt;&#x2F;p&gt;
2083&lt;p&gt;Let&#x27;s do that:&lt;&#x2F;p&gt;
2084&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&#x2F;&#x2F; new vector to hold the locations, before getting into `memory.windows`&#x27; for-each
2085let mut locations = Vec::with_capacity(regions.len());
2086
2087&#x2F;&#x2F; -snip-
2088
2089&#x2F;&#x2F; updating the `println!(&amp;quot;Found exact value...&amp;quot;)` to store the location instead.
2090if window == target {
2091    locations.push(region.BaseAddress as usize + offset);
2092}
2093
2094&#x2F;&#x2F; -snip-
2095
2096&#x2F;&#x2F; performing a second scan on the locations the first scan found.
2097let target: i32 = ...;
2098let target = target.to_ne_bytes();
2099locations.retain(|addr| match process.read_memory(*addr, target.len()) {
2100    Ok(memory) =&amp;gt; memory == target,
2101    Err(_) =&amp;gt; false,
2102});
2103
2104println!(&amp;quot;Now have {} locations&amp;quot;, locations.len());
2105&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2106&lt;p&gt;We create a vector to store all the locations the first scan finds, and then retain those that match a second target value. You may have noticed that we perform a memory read, and thus a call to the Windows API, for every single address. With a hundred locations to read from, this is not a big deal, but oftentimes you will have tens of thousands of addresses. For the time being, we will not worry about this inefficiency, but we will get back to it once it matters:&lt;&#x2F;p&gt;
2107&lt;pre&gt;&lt;code&gt;Scanning 98 memory regions
2108Which exact value to scan for?: 100
2109Failed to read 12288 bytes at 0x13f8000: Only part of a ReadProcessMemory or WriteProcessMemory request was completed. (os error 299)
2110...
2111Found 49 locations
2112Which exact value to scan for next?: 99
2113Now have 1 locations
2114&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2115&lt;p&gt;Sweet! In a real-world scenario, you will likely need to perform these additional scans a couple of times, and even then, there may be more than one value left no matter what.&lt;&#x2F;p&gt;
2116&lt;p&gt;For good measure, we&#x27;ll wrap our &lt;code&gt;retain&lt;&#x2F;code&gt; in a &lt;code&gt;while&lt;&#x2F;code&gt; loop&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#8&quot;&gt;8&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;:&lt;&#x2F;p&gt;
2117&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;while locations.len() != 1 {
2118    let target: i32 = ...;
2119    let target = target.to_ne_bytes();
2120    locations.retain(...);
2121}
2122&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2123&lt;h2 id=&quot;modifying-memory&quot;&gt;Modifying memory&lt;&#x2F;h2&gt;
2124&lt;p&gt;Now that we have very likely locations pointing to our current health in memory, all that&#x27;s left is writing our new desired value to gain infinite health&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#9&quot;&gt;9&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. Much like how we&#x27;re able to read memory with &lt;code&gt;ReadProcessMemory&lt;&#x2F;code&gt;, we can write to it with &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;memoryapi&#x2F;nf-memoryapi-writeprocessmemory&quot;&gt;&lt;code&gt;WriteProcessMemory&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. Its usage is straightforward:&lt;&#x2F;p&gt;
2125&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;pub fn write_memory(&amp;amp;self, addr: usize, value: &amp;amp;[u8]) -&amp;gt; io::Result&amp;lt;usize&amp;gt; {
2126    let mut written = 0;
2127
2128    &#x2F;&#x2F; SAFETY: the input value buffer points to valid memory.
2129    if unsafe {
2130        winapi::um::memoryapi::WriteProcessMemory(
2131            self.handle.as_ptr(),
2132            addr as *mut _,
2133            value.as_ptr().cast(),
2134            value.len(),
2135            &amp;amp;mut written,
2136        )
2137    } == FALSE
2138    {
2139        Err(io::Error::last_os_error())
2140    } else {
2141        Ok(written)
2142    }
2143}
2144&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2145&lt;p&gt;Similar to how writing to a file can return short, writing to a memory location could also return short. Here we mimic the API for writing files and return the number of bytes written. The documentation indicates that we could actually ignore the amount written by passing &lt;code&gt;ptr::null_mut()&lt;&#x2F;code&gt; as the last parameter, but it does no harm to retrieve the written count as well.&lt;&#x2F;p&gt;
2146&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let new_value: i32 = ...;
2147locations
2148    .into_iter()
2149    .for_each(|addr| match process.write_memory(addr, &amp;amp;new_value) {
2150        Ok(n) =&amp;gt; eprintln!(&amp;quot;Written {} bytes to [{:x}]&amp;quot;, n, addr),
2151        Err(e) =&amp;gt; eprintln!(&amp;quot;Failed to write to [{:x}]: {}&amp;quot;, addr, e),
2152    });
2153&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2154&lt;p&gt;And just like that:&lt;&#x2F;p&gt;
2155&lt;pre&gt;&lt;code&gt;Now have 1 location(s)
2156Enter new memory value: 1000
2157Failed to write to [15d8b90]: Access is denied. (os error 5)
2158&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2159&lt;p&gt;…oh noes. Oh yeah. The documentation, which I totally didn&#x27;t forget to read, mentions:&lt;&#x2F;p&gt;
2160&lt;blockquote&gt;
2161&lt;p&gt;The handle must have &lt;code&gt;PROCESS_VM_WRITE&lt;&#x2F;code&gt; and &lt;code&gt;PROCESS_VM_OPERATION&lt;&#x2F;code&gt; access to the process.&lt;&#x2F;p&gt;
2162&lt;&#x2F;blockquote&gt;
2163&lt;p&gt;We currently open our process with &lt;code&gt;PROCESS_QUERY_INFORMATION&lt;&#x2F;code&gt; and &lt;code&gt;PROCESS_VM_READ&lt;&#x2F;code&gt;, which is enough for reading, but not for writing. Let&#x27;s adjust &lt;code&gt;OpenProcess&lt;&#x2F;code&gt; to accomodate for our new requirements:&lt;&#x2F;p&gt;
2164&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;winapi::um::processthreadsapi::OpenProcess(
2165    winnt::PROCESS_QUERY_INFORMATION
2166        | winnt::PROCESS_VM_READ
2167        | winnt::PROCESS_VM_WRITE
2168        | winnt::PROCESS_VM_OPERATION,
2169    FALSE,
2170    pid,
2171)
2172&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2173&lt;p&gt;Behold:&lt;&#x2F;p&gt;
2174&lt;pre&gt;&lt;code&gt;Now have 1 location(s)
2175Enter new memory value: 1000
2176Written 4 bytes to [15d8b90]
2177&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2178&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;user-images.githubusercontent.com&#x2F;6297805&#x2F;107829541-3f4f2d00-6d8a-11eb-87c4-e2f2d505afbc.png&quot; alt=&quot;Tutorial complete with memo&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
2179&lt;p&gt;Isn&#x27;t that active &lt;em&gt;Next&lt;&#x2F;em&gt; button just beautiful?&lt;&#x2F;p&gt;
2180&lt;h2 id=&quot;finale&quot;&gt;Finale&lt;&#x2F;h2&gt;
2181&lt;p&gt;This post somehow ended up being longer than part one, but look at what we&#x27;ve achieved! We completed a step of the Cheat Engine Tutorial &lt;em&gt;without using Cheat Engine&lt;&#x2F;em&gt;. Just pure Rust. Figuring out how a program works and reimplementing it yourself is a great way to learn what it&#x27;s doing behind the scenes. And now that this code is yours, you can extend it as much as you like, without being constrained by Cheat Engine&#x27;s UI. You can automate it as much as you want.&lt;&#x2F;p&gt;
2182&lt;p&gt;And we&#x27;re not even done. The current tutorial has nine steps, and three additional graphical levels.&lt;&#x2F;p&gt;
2183&lt;p&gt;In the &lt;a href=&quot;&#x2F;blog&#x2F;woce-3&quot;&gt;next post&lt;&#x2F;a&gt;, we&#x27;ll tackle the third step of the tutorial: Unknown initial value. This will pose a challenge, because with just 2 MiB of memory, storing all the 4-byte aligned locations would require 524288 addresses (&lt;code&gt;usize&lt;&#x2F;code&gt;, 8 bytes). This adds up to twice as much memory as the original program (4 MiB), but that&#x27;s not our main concern, having to perform over five hundred thousand API calls is!&lt;&#x2F;p&gt;
2184&lt;p&gt;Remember that you can &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lonami&#x2F;memo&quot;&gt;obtain the code for this post&lt;&#x2F;a&gt; over at my GitHub. You can run &lt;code&gt;git checkout step2&lt;&#x2F;code&gt; after cloning the repository to get the right version of the code.&lt;&#x2F;p&gt;
2185&lt;h3 id=&quot;footnotes&quot;&gt;Footnotes&lt;&#x2F;h3&gt;
2186&lt;div class=&quot;footnote-definition&quot; id=&quot;1&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;&#x2F;sup&gt;
2187&lt;p&gt;I did in fact use an online tool to spell it out for me.&lt;&#x2F;p&gt;
2188&lt;&#x2F;div&gt;
2189&lt;div class=&quot;footnote-definition&quot; id=&quot;2&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;2&lt;&#x2F;sup&gt;
2190&lt;p&gt;16 GiB is good enough for my needs. I don&#x27;t think I&#x27;ll ever upgrade to 16 EiB.&lt;&#x2F;p&gt;
2191&lt;&#x2F;div&gt;
2192&lt;div class=&quot;footnote-definition&quot; id=&quot;3&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;3&lt;&#x2F;sup&gt;
2193&lt;p&gt;Every address we query should have a corresponding region, even if it&#x27;s not allocated or we do not have access. This is why we can query for the memory address zero to get its corresponding region.&lt;&#x2F;p&gt;
2194&lt;&#x2F;div&gt;
2195&lt;div class=&quot;footnote-definition&quot; id=&quot;4&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;4&lt;&#x2F;sup&gt;
2196&lt;p&gt;Another option is to &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;sysinfoapi&#x2F;nf-sysinfoapi-getsysteminfo&quot;&gt;&lt;code&gt;GetSystemInfo&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; to determine the &lt;code&gt;lpMinimumApplicationAddress&lt;&#x2F;code&gt; and &lt;code&gt;lpMaximumApplicationAddress&lt;&#x2F;code&gt; and only work within bounds.&lt;&#x2F;p&gt;
2197&lt;&#x2F;div&gt;
2198&lt;div class=&quot;footnote-definition&quot; id=&quot;5&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;5&lt;&#x2F;sup&gt;
2199&lt;p&gt;Memory regions are page-aligned, which is a large power of two. Our alignment of 4 is much lower than this, so we&#x27;re guaranteed to start off at an aligned address.&lt;&#x2F;p&gt;
2200&lt;&#x2F;div&gt;
2201&lt;div class=&quot;footnote-definition&quot; id=&quot;6&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;6&lt;&#x2F;sup&gt;
2202&lt;p&gt;If it turns out that the value was actually misaligned, we will miss it. You will notice this if, after going through the whole process, there are no results. It could mean that either the value type is wrong, or the value type is misaligned. In the worst case, the value is not stored directly but is rather computed with something like &lt;code&gt;maximum - stored&lt;&#x2F;code&gt;, or XORed with some magic value, or a myriad other things.&lt;&#x2F;p&gt;
2203&lt;&#x2F;div&gt;
2204&lt;div class=&quot;footnote-definition&quot; id=&quot;7&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;7&lt;&#x2F;sup&gt;
2205&lt;p&gt;You could do this without getting hit, and just keep on repeating the scan for the same value over and over again. This does work, but the results are suboptimal, because there are also many other values that didn&#x27;t change. Scanning for a changed value is a better option.&lt;&#x2F;p&gt;
2206&lt;&#x2F;div&gt;
2207&lt;div class=&quot;footnote-definition&quot; id=&quot;8&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;8&lt;&#x2F;sup&gt;
2208&lt;p&gt;You could actually just go ahead and try to modify the memory at the hundred addresses you just found, although don&#x27;t be surprised if the program starts to misbehave!&lt;&#x2F;p&gt;
2209&lt;&#x2F;div&gt;
2210&lt;div class=&quot;footnote-definition&quot; id=&quot;9&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;9&lt;&#x2F;sup&gt;
2211&lt;p&gt;Okay, we cannot fit infinity in an &lt;code&gt;i32&lt;&#x2F;code&gt;. However, we can fit sufficiently large numbers. Like &lt;code&gt;1000&lt;&#x2F;code&gt;, which is enough to complete the tutorial.&lt;&#x2F;p&gt;
2212&lt;&#x2F;div&gt;
2213</content>
2214	</entry>
2215	<entry xml:lang="en">
2216		<title>Writing our own Cheat Engine: Introduction</title>
2217		<published>2021-02-07T00:00:00+00:00</published>
2218		<updated>2021-02-19T00:00:00+00:00</updated>
2219		<link href="https://lonami.dev/blog/woce-1/" type="text/html"/>
2220		<id>https://lonami.dev/blog/woce-1/</id>
2221		<content type="html">&lt;p&gt;This is part 1 on the &lt;em&gt;Writing our own Cheat Engine&lt;&#x2F;em&gt; series:&lt;&#x2F;p&gt;
2222&lt;ul&gt;
2223&lt;li&gt;Part 1: Introduction&lt;&#x2F;li&gt;
2224&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-2&quot;&gt;Part 2: Exact Value scanning&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
2225&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-3&quot;&gt;Part 3: Unknown initial value&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
2226&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-4&quot;&gt;Part 4: Floating points&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
2227&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-5&quot;&gt;Part 5: Code finder&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
2228&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;woce-6&quot;&gt;Part 6: Pointers&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
2229&lt;&#x2F;ul&gt;
2230&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;cheatengine.org&#x2F;&quot;&gt;Cheat Engine&lt;&#x2F;a&gt; is a tool designed to modify single player games and contains other useful tools within itself that enable its users to debug games or other applications. It comes with a memory scanner, (dis)assembler, inspection tools and a handful other things. In this series, we will be writing our own tiny Cheat Engine capable of solving all steps of the tutorial, and diving into how it all works underneath.&lt;&#x2F;p&gt;
2231&lt;p&gt;Needless to say, we&#x27;re doing this for private and educational purposes only. One has to make sure to not violate the EULA or ToS of the specific application we&#x27;re attaching to. This series, much like cheatengine.org, does not condone the illegal use of the code shared.&lt;&#x2F;p&gt;
2232&lt;p&gt;Cheat Engine is a tool for Windows, so we will be developing for Windows as well. However, you can also &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;q&#x2F;12977179&#x2F;4759433&quot;&gt;read memory from Linux-like systems&lt;&#x2F;a&gt;. &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;scanmem&#x2F;scanmem&quot;&gt;GameConqueror&lt;&#x2F;a&gt; is a popular alternative to Cheat Engine on Linux systems, so if you feel adventurous, you could definitely follow along too! The techniques shown in this series apply regardless of how we read memory from a process. You will learn a fair bit about doing FFI in Rust too.&lt;&#x2F;p&gt;
2233&lt;p&gt;We will be developing the application in Rust, because it enables us to interface with the Windows API easily, is memory safe (as long as we&#x27;re careful with &lt;code&gt;unsafe&lt;&#x2F;code&gt;!), and is speedy (we will need this for later steps in the Cheat Engine tutorial). You could use any language of your choice though. For example, &lt;a href=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;ctypes-and-windows&#x2F;&quot;&gt;Python also makes it relatively easy to use the Windows API&lt;&#x2F;a&gt;. You don&#x27;t need to be a Rust expert to follow along, but this series assumes some familiarity with C-family languages. Slightly advanced concepts like the use of &lt;code&gt;unsafe&lt;&#x2F;code&gt; or the &lt;code&gt;MaybeUninit&lt;&#x2F;code&gt; type will be briefly explained. What a &lt;code&gt;fn&lt;&#x2F;code&gt; is or what &lt;code&gt;let&lt;&#x2F;code&gt; does will not be explained.&lt;&#x2F;p&gt;
2234&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;cheat-engine&#x2F;cheat-engine&#x2F;&quot;&gt;Cheat Engine&#x27;s source code&lt;&#x2F;a&gt; is mostly written in Pascal and C. And it&#x27;s &lt;em&gt;a lot&lt;&#x2F;em&gt; of code, with a very flat project structure, and files ranging in the thousand lines of code each. It&#x27;s daunting&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. It&#x27;s a mature project, with a lot of knowledge encoded in the code base, and a lot of features like distributed scanning or an entire disassembler. Unfortunately, there&#x27;s not a lot of comments. For these reasons, I&#x27;ll do some guesswork when possible as to how it&#x27;s working underneath, rather than actually digging into what Cheat Engine is actually doing.&lt;&#x2F;p&gt;
2235&lt;p&gt;With that out of the way, let&#x27;s get started!&lt;&#x2F;p&gt;
2236&lt;h2 id=&quot;welcome-to-the-cheat-engine-tutorial&quot;&gt;Welcome to the Cheat Engine Tutorial&lt;&#x2F;h2&gt;
2237&lt;details open&gt;&lt;summary&gt;Cheat Engine Tutorial: Step 1&lt;&#x2F;summary&gt;
2238&lt;blockquote&gt;
2239&lt;p&gt;This tutorial will teach you the basics of cheating in video games. It will also show you foundational aspects of using Cheat Engine (or CE for short). Follow the steps below to get started.&lt;&#x2F;p&gt;
2240&lt;ol&gt;
2241&lt;li&gt;Open Cheat Engine if it currently isn&#x27;t running.&lt;&#x2F;li&gt;
2242&lt;li&gt;Click on the &amp;quot;Open Process&amp;quot; icon (it&#x27;s the top-left icon with the computer on it, below &amp;quot;File&amp;quot;.).&lt;&#x2F;li&gt;
2243&lt;li&gt;With the Process List window now open, look for this tutorial&#x27;s process in the list. It will look something like &amp;gt; &amp;quot;00001F98-Tutorial-x86_64.exe&amp;quot; or &amp;quot;0000047C-Tutorial-i386.exe&amp;quot;. (The first 8 numbers&#x2F;letters will probably be different.)&lt;&#x2F;li&gt;
2244&lt;li&gt;Once you&#x27;ve found the process, click on it to select it, then click the &amp;quot;Open&amp;quot; button. (Don&#x27;t worry about all the &amp;gt; other buttons right now. You can learn about them later if you&#x27;re interested.)&lt;&#x2F;li&gt;
2245&lt;&#x2F;ol&gt;
2246&lt;p&gt;Congratulations! If you did everything correctly, the process window should be gone with Cheat Engine now attached to the &amp;gt; tutorial (you will see the process name towards the top-center of CE).&lt;&#x2F;p&gt;
2247&lt;p&gt;Click the &amp;quot;Next&amp;quot; button below to continue, or fill in the password and click the &amp;quot;OK&amp;quot; button to proceed to that step.)&lt;&#x2F;p&gt;
2248&lt;p&gt;If you&#x27;re having problems, simply head over to forum.cheatengine.org, then click on &amp;quot;Tutorials&amp;quot; to view beginner-friendly &amp;gt; guides!&lt;&#x2F;p&gt;
2249&lt;&#x2F;blockquote&gt;
2250&lt;&#x2F;details&gt;
2251&lt;h2 id=&quot;enumerating-processes&quot;&gt;Enumerating processes&lt;&#x2F;h2&gt;
2252&lt;p&gt;Our first step is attaching to the process we want to work with. But we need a way to find that process in the first place! Having to open the task manager, look for the process we care about, noting down the process ID (PID), and slapping it in the source code is not satisfying at all. Instead, let&#x27;s enumerate all the processes from within the program, and let the user select one by typing its name.&lt;&#x2F;p&gt;
2253&lt;p&gt;From a quick &lt;a href=&quot;https:&#x2F;&#x2F;ddg.gg&#x2F;winapi%20enumerate%20all%20processes&quot;&gt;DuckDuckGo search&lt;&#x2F;a&gt;, we find an official tutorial for &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;psapi&#x2F;enumerating-all-processes&quot;&gt;Enumerating All Processes&lt;&#x2F;a&gt;, which leads to the &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;psapi&#x2F;nf-psapi-enumprocesses&quot;&gt;&lt;code&gt;EnumProcesses&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; call. Cool! Let&#x27;s slap in the &lt;a href=&quot;https:&#x2F;&#x2F;crates.io&#x2F;crates&#x2F;winapi&quot;&gt;&lt;code&gt;winapi&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; crate on &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;, because I don&#x27;t want to write all the definitions by myself:&lt;&#x2F;p&gt;
2254&lt;pre&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;[dependencies]
2255winapi = { version = &amp;quot;0.3.9&amp;quot;, features = [&amp;quot;psapi&amp;quot;] }
2256&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2257&lt;p&gt;Because &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;psapi&#x2F;nf-psapi-enumprocesses&quot;&gt;&lt;code&gt;EnumProcesses&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; is in &lt;code&gt;Psapi.h&lt;&#x2F;code&gt; (you can see this in the online page of its documentation), we know we&#x27;ll need the &lt;code&gt;psapi&lt;&#x2F;code&gt; crate feature. Another option is to search for it in the &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;winapi&#x2F;&quot;&gt;&lt;code&gt;winapi&lt;&#x2F;code&gt; documentation&lt;&#x2F;a&gt; and noting down the parent module where its stored.&lt;&#x2F;p&gt;
2258&lt;p&gt;The documentation for the method has the following remark:&lt;&#x2F;p&gt;
2259&lt;blockquote&gt;
2260&lt;p&gt;It is a good idea to use a large array, because it is hard to predict how many processes there will be at the time you call &lt;strong&gt;EnumProcesses&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
2261&lt;&#x2F;blockquote&gt;
2262&lt;p&gt;&lt;em&gt;Sidenote: reading the documentation for the methods we&#x27;ll use from the Windows API is extremely important. There&#x27;s a lot of gotchas involved, so we need to make sure we&#x27;re extra careful.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
2263&lt;p&gt;1024 is a pretty big number, so let&#x27;s go with that:&lt;&#x2F;p&gt;
2264&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;use std::io;
2265use std::mem;
2266use winapi::shared::minwindef::{DWORD, FALSE};
2267
2268pub fn enum_proc() -&amp;gt; io::Result&amp;lt;Vec&amp;lt;u32&amp;gt;&amp;gt; {
2269    let mut pids = Vec::&amp;lt;DWORD&amp;gt;::with_capacity(1024);
2270    let mut size = 0;
2271    &#x2F;&#x2F; SAFETY: the pointer is valid and the size matches the capacity.
2272    if unsafe {
2273        winapi::um::psapi::EnumProcesses(
2274            pids.as_mut_ptr(),
2275            (pids.capacity() * mem::size_of::&amp;lt;DWORD&amp;gt;()) as u32,
2276            &amp;amp;mut size,
2277        )
2278    } == FALSE
2279    {
2280        return Err(io::Error::last_os_error());
2281    }
2282
2283    todo!()
2284}
2285&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2286&lt;p&gt;We allocate enough space&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#2&quot;&gt;2&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; for 1024 &lt;code&gt;pids&lt;&#x2F;code&gt; in a vector&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#3&quot;&gt;3&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;, and pass a mutable pointer to the contents to &lt;code&gt;EnumProcesses&lt;&#x2F;code&gt;. Note that the size of the array is in &lt;em&gt;bytes&lt;&#x2F;em&gt;, not items, so we need to multiply the capacity by the size of &lt;code&gt;DWORD&lt;&#x2F;code&gt;. The API likes to use &lt;code&gt;u32&lt;&#x2F;code&gt; for sizes, unlike Rust which uses &lt;code&gt;usize&lt;&#x2F;code&gt;, so we need a cast.&lt;&#x2F;p&gt;
2287&lt;p&gt;Last, we need another mutable variable where the amount of bytes written is stored, &lt;code&gt;size&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
2288&lt;blockquote&gt;
2289&lt;p&gt;If the function fails, the return value is zero. To get extended error information, call &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;errhandlingapi&#x2F;nf-errhandlingapi-getlasterror&quot;&gt;&lt;code&gt;GetLastError&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
2290&lt;&#x2F;blockquote&gt;
2291&lt;p&gt;That&#x27;s precisely what we do. If it returns false (zero), we return the last OS error. Rust provides us with &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;std&#x2F;io&#x2F;struct.Error.html#method.last_os_error&quot;&gt;&lt;code&gt;std::io::Error::last_os_error&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, which essentially makes that same call but returns a proper &lt;code&gt;io::Error&lt;&#x2F;code&gt; instance. Cool!&lt;&#x2F;p&gt;
2292&lt;blockquote&gt;
2293&lt;p&gt;To determine how many processes were enumerated, divide the &lt;em&gt;lpcbNeeded&lt;&#x2F;em&gt; value by &lt;code&gt;sizeof(DWORD)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
2294&lt;&#x2F;blockquote&gt;
2295&lt;p&gt;Easy enough:&lt;&#x2F;p&gt;
2296&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let count = size as usize &#x2F; mem::size_of::&amp;lt;DWORD&amp;gt;();
2297&#x2F;&#x2F; SAFETY: the call succeeded and count equals the right amount of items.
2298unsafe { pids.set_len(count) };
2299Ok(pids)
2300&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2301&lt;p&gt;Rust doesn&#x27;t know that the memory for &lt;code&gt;count&lt;&#x2F;code&gt; items were initialized by the call, but we do, so we make use of the &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;std&#x2F;vec&#x2F;struct.Vec.html#method.set_len&quot;&gt;&lt;code&gt;Vec::set_len&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; call to indicate this. The Rust documentation even includes a FFI similar to our code!&lt;&#x2F;p&gt;
2302&lt;p&gt;Let&#x27;s give it a ride:&lt;&#x2F;p&gt;
2303&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;fn main() {
2304    dbg!(enum_proc().unwrap().len());
2305}
2306&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2307&lt;pre&gt;&lt;code&gt;&amp;gt;cargo run
2308   Compiling memo v0.1.0
2309    Finished dev [unoptimized + debuginfo] target(s) in 0.20s
2310     Running `target\debug\memo.exe`
2311[src\main.rs:27] enum_proc().unwrap().len() = 178
2312&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2313&lt;p&gt;It works! But currently we only have a bunch of process identifiers, with no way of knowing which process they refer to.&lt;&#x2F;p&gt;
2314&lt;blockquote&gt;
2315&lt;p&gt;To obtain process handles for the processes whose identifiers you have just obtained, call the &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;processthreadsapi&#x2F;nf-processthreadsapi-openprocess&quot;&gt;&lt;code&gt;OpenProcess&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; function.&lt;&#x2F;p&gt;
2316&lt;&#x2F;blockquote&gt;
2317&lt;p&gt;Oh!&lt;&#x2F;p&gt;
2318&lt;h2 id=&quot;opening-a-process&quot;&gt;Opening a process&lt;&#x2F;h2&gt;
2319&lt;p&gt;The documentation for &lt;code&gt;OpenProcess&lt;&#x2F;code&gt; also contains the following:&lt;&#x2F;p&gt;
2320&lt;blockquote&gt;
2321&lt;p&gt;When you are finished with the handle, be sure to close it using the &lt;a href=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;woce-1&#x2F;closehandle&quot;&gt;&lt;code&gt;CloseHandle&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; function.&lt;&#x2F;p&gt;
2322&lt;&#x2F;blockquote&gt;
2323&lt;p&gt;This sounds to me like the perfect time to introduce a custom &lt;code&gt;struct Process&lt;&#x2F;code&gt; with an &lt;code&gt;impl Drop&lt;&#x2F;code&gt;! We&#x27;re using &lt;code&gt;Drop&lt;&#x2F;code&gt; to cleanup resources, not behaviour, so it&#x27;s fine. &lt;a href=&quot;https:&#x2F;&#x2F;internals.rust-lang.org&#x2F;t&#x2F;pre-rfc-leave-auto-trait-for-reliable-destruction&#x2F;13825&quot;&gt;Using &lt;code&gt;Drop&lt;&#x2F;code&gt; to cleanup behaviour is a bad idea&lt;&#x2F;a&gt;. But anyway, let&#x27;s get back to the code:&lt;&#x2F;p&gt;
2324&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;use std::ptr::NonNull;
2325use winapi::ctypes::c_void;
2326
2327pub struct Process {
2328    pid: u32,
2329    handle: NonNull&amp;lt;c_void&amp;gt;,
2330}
2331
2332impl Process {
2333    pub fn open(pid: u32) -&amp;gt; io::Result&amp;lt;Self&amp;gt; {
2334        todo!()
2335    }
2336}
2337
2338impl Drop for Process {
2339    fn drop(&amp;amp;mut self) {
2340        todo!()
2341    }
2342}
2343&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2344&lt;p&gt;For &lt;code&gt;open&lt;&#x2F;code&gt;, we&#x27;ll want to use &lt;code&gt;OpenProcess&lt;&#x2F;code&gt; (and we also need to add the &lt;code&gt;processthreadsapi&lt;&#x2F;code&gt; feature to the &lt;code&gt;winapi&lt;&#x2F;code&gt; dependency in &lt;code&gt;Cargo.toml&lt;&#x2F;code&gt;). It returns a &lt;code&gt;HANDLE&lt;&#x2F;code&gt;, which is a nullable mutable pointer to &lt;code&gt;c_void&lt;&#x2F;code&gt;. If it&#x27;s null, the call failed, and if it&#x27;s non-null, it succeeded and we have a valid handle. This is why we use Rust&#x27;s &lt;a href=&quot;https:&#x2F;&#x2F;doc.rust-lang.org&#x2F;stable&#x2F;std&#x2F;ptr&#x2F;struct.NonNull.html&quot;&gt;&lt;code&gt;NonNull&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
2345&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&#x2F;&#x2F; SAFETY: the call doesn&#x27;t have dangerous side-effects.
2346NonNull::new(unsafe { winapi::um::processthreadsapi::OpenProcess(0, FALSE, pid) })
2347    .map(|handle| Self { pid, handle })
2348    .ok_or_else(io::Error::last_os_error)
2349&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2350&lt;p&gt;&lt;code&gt;NonNull&lt;&#x2F;code&gt; will return &lt;code&gt;Some&lt;&#x2F;code&gt; if the pointer is non-null. We map the non-null pointer to a &lt;code&gt;Process&lt;&#x2F;code&gt; instance with &lt;code&gt;Self { .. }&lt;&#x2F;code&gt;. &lt;code&gt;ok_or_else&lt;&#x2F;code&gt; converts the &lt;code&gt;Option&lt;&#x2F;code&gt; to a &lt;code&gt;Result&lt;&#x2F;code&gt; with the error builder function we provide if it was &lt;code&gt;None&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
2351&lt;p&gt;The first parameter is a bitflag of permissions we want to have. For now, we can leave it as zero (all bits unset, no specific permissions granted). The second one is whether we want to inherit the handle, which we don&#x27;t, and the third one is the process identifier. Let&#x27;s close the resource handle on &lt;code&gt;Drop&lt;&#x2F;code&gt; (after adding &lt;code&gt;handleapi&lt;&#x2F;code&gt; to the crate features):&lt;&#x2F;p&gt;
2352&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&#x2F;&#x2F; SAFETY: the handle is valid and non-null.
2353unsafe { winapi::um::handleapi::CloseHandle(self.handle.as_mut()) };
2354&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2355&lt;p&gt;&lt;code&gt;CloseHandle&lt;&#x2F;code&gt; can actually fail (for example, on double-close), but given our invariants, it won&#x27;t. You could add an &lt;code&gt;assert!&lt;&#x2F;code&gt; to panic if this is not the case.&lt;&#x2F;p&gt;
2356&lt;p&gt;We can now open processes, and they will be automatically closed on &lt;code&gt;Drop&lt;&#x2F;code&gt;. Does any of this work though?&lt;&#x2F;p&gt;
2357&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;fn main() {
2358    let mut success = 0;
2359    let mut failed = 0;
2360    enum_proc().unwrap().into_iter().for_each(|pid| match Process::open(pid) {
2361        Ok(_) =&amp;gt; success += 1,
2362        Err(_) =&amp;gt; failed += 1,
2363    });
2364
2365    eprintln!(&amp;quot;Successfully opened {}&#x2F;{} processes&amp;quot;, success, success + failed);
2366}
2367&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2368&lt;pre&gt;&lt;code&gt;&amp;gt;cargo run
2369   Compiling memo v0.1.0
2370    Finished dev [unoptimized + debuginfo] target(s) in 0.36s
2371     Running `target\debug\memo.exe`
2372Successfully opened 0&#x2F;191 processes
2373&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2374&lt;p&gt;…nope. Maybe the documentation for &lt;code&gt;OpenProcess&lt;&#x2F;code&gt; says something?&lt;&#x2F;p&gt;
2375&lt;blockquote&gt;
2376&lt;p&gt;&lt;code&gt;dwDesiredAccess&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
2377&lt;p&gt;The access to the process object. This access right is checked against the security descriptor for the process. This parameter can be &lt;strong&gt;one or more&lt;&#x2F;strong&gt; of the process access rights.&lt;&#x2F;p&gt;
2378&lt;&#x2F;blockquote&gt;
2379&lt;p&gt;One or more, but we&#x27;re setting zero permissions. I told you, reading the documentation is important&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#4&quot;&gt;4&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;! The &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;procthread&#x2F;process-security-and-access-rights&quot;&gt;Process Security and Access Rights&lt;&#x2F;a&gt; page lists all possible values we could use. &lt;code&gt;PROCESS_QUERY_INFORMATION&lt;&#x2F;code&gt; seems to be appropriated:&lt;&#x2F;p&gt;
2380&lt;blockquote&gt;
2381&lt;p&gt;Required to retrieve certain information about a process, such as its token, exit code, and priority class&lt;&#x2F;p&gt;
2382&lt;&#x2F;blockquote&gt;
2383&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;OpenProcess(winapi::um::winnt::PROCESS_QUERY_INFORMATION, ...)
2384&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2385&lt;p&gt;Does this fix it?&lt;&#x2F;p&gt;
2386&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&amp;gt;cargo run
2387   Compiling memo v0.1.0
2388    Finished dev [unoptimized + debuginfo] target(s) in 0.36s
2389     Running `target\debug\memo.exe`
2390Successfully opened 69&#x2F;188 processes
2391&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2392&lt;p&gt;&lt;em&gt;Nice&lt;&#x2F;em&gt;. It does solve it. But why did we only open 69 processes out of 188? Does it help if we run our code as administrator? Let&#x27;s search for &lt;code&gt;cmd&lt;&#x2F;code&gt; in the Windows menu and right click to Run as administrator, then &lt;code&gt;cd&lt;&#x2F;code&gt; into our project and try again:&lt;&#x2F;p&gt;
2393&lt;pre&gt;&lt;code&gt;&amp;gt;cargo run
2394    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
2395     Running `target\debug\memo.exe`
2396Successfully opened 77&#x2F;190 processes
2397&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2398&lt;p&gt;We&#x27;re able to open a few more, so it does help. In general, we&#x27;ll want to run as administrator, so normal programs can&#x27;t sniff on what we&#x27;re doing, and so that we have permission to do more things.&lt;&#x2F;p&gt;
2399&lt;h2 id=&quot;getting-the-name-of-a-process&quot;&gt;Getting the name of a process&lt;&#x2F;h2&gt;
2400&lt;p&gt;We&#x27;re not done enumerating things just yet. To get the &amp;quot;name&amp;quot; of a process, we need to enumerate the modules that it has loaded, and only then can we get the module base name. The first module is the program itself, so we don&#x27;t need to enumerate &lt;em&gt;all&lt;&#x2F;em&gt; modules, just the one is enough.&lt;&#x2F;p&gt;
2401&lt;p&gt;For this we want &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;psapi&#x2F;nf-psapi-enumprocessmodules&quot;&gt;&lt;code&gt;EnumProcessModules&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;psapi&#x2F;nf-psapi-getmodulebasenamea&quot;&gt;&lt;code&gt;GetModuleBaseNameA&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. I&#x27;m using the ASCII variant of &lt;code&gt;GetModuleBaseName&lt;&#x2F;code&gt; because I&#x27;m too lazy to deal with UTF-16 of the &lt;code&gt;W&lt;&#x2F;code&gt; (wide, unicode) variants.&lt;&#x2F;p&gt;
2402&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;use std::mem::MaybeUninit;
2403use winapi::shared::minwindef::HMODULE;
2404
2405pub fn name(&amp;amp;self) -&amp;gt; io::Result&amp;lt;String&amp;gt; {
2406    let mut module = MaybeUninit::&amp;lt;HMODULE&amp;gt;::uninit();
2407    let mut size = 0;
2408    &#x2F;&#x2F; SAFETY: the pointer is valid and the size is correct.
2409    if unsafe {
2410        winapi::um::psapi::EnumProcessModules(
2411            self.handle.as_ptr(),
2412            module.as_mut_ptr(),
2413            mem::size_of::&amp;lt;HMODULE&amp;gt;() as u32,
2414            &amp;amp;mut size,
2415        )
2416    } == FALSE
2417    {
2418        return Err(io::Error::last_os_error());
2419    }
2420
2421    &#x2F;&#x2F; SAFETY: the call succeeded, so module is initialized.
2422    let module = unsafe { module.assume_init() };
2423    todo!()
2424}
2425&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2426&lt;p&gt;&lt;code&gt;EnumProcessModules&lt;&#x2F;code&gt; takes a pointer to an array of &lt;code&gt;HMODULE&lt;&#x2F;code&gt;. We could use a &lt;code&gt;Vec&lt;&#x2F;code&gt; of capacity one to hold the single module, but in memory, a pointer a single item can be seen as a pointer to an array of items. &lt;code&gt;MaybeUninit&lt;&#x2F;code&gt; helps us reserve enough memory for the one item we need.&lt;&#x2F;p&gt;
2427&lt;p&gt;With the module handle, we can retrieve its base name:&lt;&#x2F;p&gt;
2428&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;let mut buffer = Vec::&amp;lt;u8&amp;gt;::with_capacity(64);
2429&#x2F;&#x2F; SAFETY: the handle, module and buffer are all valid.
2430let length = unsafe {
2431    winapi::um::psapi::GetModuleBaseNameA(
2432        self.handle.as_ptr(),
2433        module,
2434        buffer.as_mut_ptr().cast(),
2435        buffer.capacity() as u32,
2436    )
2437};
2438if length == 0 {
2439    return Err(io::Error::last_os_error());
2440}
2441
2442&#x2F;&#x2F; SAFETY: the call succeeded and length represents bytes.
2443unsafe { buffer.set_len(length as usize) };
2444Ok(String::from_utf8(buffer).unwrap())
2445&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2446&lt;p&gt;Similar to how we did with &lt;code&gt;EnumProcesses&lt;&#x2F;code&gt;, we create a buffer that will hold the ASCII string of the module&#x27;s base name&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#5&quot;&gt;5&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. The call wants us to pass a pointer to a mutable buffer of &lt;code&gt;i8&lt;&#x2F;code&gt;, but Rust&#x27;s &lt;code&gt;String::from_utf8&lt;&#x2F;code&gt; wants a &lt;code&gt;Vec&amp;lt;u8&amp;gt;&lt;&#x2F;code&gt;, so instead we declare a buffer of &lt;code&gt;u8&lt;&#x2F;code&gt; and &lt;code&gt;.cast()&lt;&#x2F;code&gt; the pointer in the call. You could also do this with &lt;code&gt;as _&lt;&#x2F;code&gt;, and Rust would infer the right type, but &lt;code&gt;cast&lt;&#x2F;code&gt; is neat.&lt;&#x2F;p&gt;
2447&lt;p&gt;We &lt;code&gt;unwrap&lt;&#x2F;code&gt; the creation of the UTF-8 string because the buffer should contain only ASCII characters (which are also valid UTF-8). We could use the &lt;code&gt;unsafe&lt;&#x2F;code&gt; variant to create the string, but what if somehow it contains non-ASCII characters? The less &lt;code&gt;unsafe&lt;&#x2F;code&gt;, the better.&lt;&#x2F;p&gt;
2448&lt;p&gt;Let&#x27;s see it in action:&lt;&#x2F;p&gt;
2449&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;fn main() {
2450    enum_proc()
2451        .unwrap()
2452        .into_iter()
2453        .for_each(|pid| match Process::open(pid) {
2454            Ok(proc) =&amp;gt; match proc.name() {
2455                Ok(name) =&amp;gt; println!(&amp;quot;{}: {}&amp;quot;, pid, name),
2456                Err(e) =&amp;gt; println!(&amp;quot;{}: (failed to get name: {})&amp;quot;, pid, e),
2457            },
2458            Err(e) =&amp;gt; eprintln!(&amp;quot;failed to open {}: {}&amp;quot;, pid, e),
2459        });
2460}
2461&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2462&lt;pre&gt;&lt;code&gt;&amp;gt;cargo run
2463   Compiling memo v0.1.0
2464    Finished dev [unoptimized + debuginfo] target(s) in 0.32s
2465     Running `target\debug\memo.exe`
2466failed to open 0: The parameter is incorrect. (os error 87)
2467failed to open 4: Access is denied. (os error 5)
2468...
2469failed to open 5940: Access is denied. (os error 5)
24705608: (failed to get name: Access is denied. (os error 5))
2471...
24721704: (failed to get name: Access is denied. (os error 5))
2473failed to open 868: Access is denied. (os error 5)
2474...
2475&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2476&lt;p&gt;That&#x27;s not good. What&#x27;s up with that? Maybe…&lt;&#x2F;p&gt;
2477&lt;blockquote&gt;
2478&lt;p&gt;The handle must have the &lt;code&gt;PROCESS_QUERY_INFORMATION&lt;&#x2F;code&gt; and &lt;code&gt;PROCESS_VM_READ&lt;&#x2F;code&gt; access rights.&lt;&#x2F;p&gt;
2479&lt;&#x2F;blockquote&gt;
2480&lt;p&gt;…I should&#x27;ve read the documentation. Okay, fine:&lt;&#x2F;p&gt;
2481&lt;pre&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;use winapi::um::winnt;
2482OpenProcess(winnt::PROCESS_QUERY_INFORMATION | winnt::PROCESS_VM_READ, ...)
2483&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2484&lt;pre&gt;&lt;code&gt;&amp;gt;cargo run
2485   Compiling memo v0.1.0 (C:\Users\L\Desktop\memo)
2486    Finished dev [unoptimized + debuginfo] target(s) in 0.35s
2487     Running `target\debug\memo.exe`
2488failed to open 0: The parameter is incorrect. (os error 87)
2489failed to open 4: Access is denied. (os error 5)
2490...
24919348: cheatengine-x86_64.exe
24923288: Tutorial-x86_64.exe
24938396: cmd.exe
24944620: firefox.exe
24957964: cargo.exe
249610052: cargo.exe
24975756: memo.exe
2498&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2499&lt;p&gt;Hooray 🎉! There&#x27;s some processes we can&#x27;t open, but that&#x27;s because they&#x27;re system processes. Security works!&lt;&#x2F;p&gt;
2500&lt;h2 id=&quot;finale&quot;&gt;Finale&lt;&#x2F;h2&gt;
2501&lt;p&gt;That was a fairly long post when all we did was print a bunch of pids and their corresponding name. But in all fairness, we also laid out a good foundation for what&#x27;s coming next.&lt;&#x2F;p&gt;
2502&lt;p&gt;You can &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;lonami&#x2F;memo&quot;&gt;obtain the code for this post&lt;&#x2F;a&gt; over at my GitHub. At the end of every post, the last commit will be tagged, so you can &lt;code&gt;git checkout step1&lt;&#x2F;code&gt; to see the final code for any blog post.&lt;&#x2F;p&gt;
2503&lt;p&gt;In the &lt;a href=&quot;&#x2F;blog&#x2F;woce-2&quot;&gt;next post&lt;&#x2F;a&gt;, we&#x27;ll tackle the second step of the tutorial: Exact Value scanning.&lt;&#x2F;p&gt;
2504&lt;h3 id=&quot;footnotes&quot;&gt;Footnotes&lt;&#x2F;h3&gt;
2505&lt;div class=&quot;footnote-definition&quot; id=&quot;1&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;&#x2F;sup&gt;
2506&lt;p&gt;You could say I simply love reinventing the wheel, which I do, but in this case, the codebase contains &lt;em&gt;far&lt;&#x2F;em&gt; more features than we&#x27;re interested in. The (apparent) lack of structure and documentation regarding the code, along with the unfortunate &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;cheat-engine&#x2F;cheat-engine&#x2F;issues&#x2F;60&quot;&gt;lack of license&lt;&#x2F;a&gt; for the source code, make it a no-go. There&#x27;s a license, but I think that&#x27;s for the distributed program itself.&lt;&#x2F;p&gt;
2507&lt;&#x2F;div&gt;
2508&lt;div class=&quot;footnote-definition&quot; id=&quot;2&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;2&lt;&#x2F;sup&gt;
2509&lt;p&gt;If it turns out that there are more than 1024 processes, our code will be unaware of those extra processes. The documentation suggests to perform the call again with a larger buffer if &lt;code&gt;count == provided capacity&lt;&#x2F;code&gt;, but given I have under 200 processes on my system, it seems unlikely we&#x27;ll reach this limit. If you&#x27;re worried about hitting this limit, simply use a larger limit or retry with a larger vector.&lt;&#x2F;p&gt;
2510&lt;&#x2F;div&gt;
2511&lt;div class=&quot;footnote-definition&quot; id=&quot;3&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;3&lt;&#x2F;sup&gt;
2512&lt;p&gt;C code would likely use &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;winbase&#x2F;nf-winbase-globalalloc&quot;&gt;&lt;code&gt;GlobalAlloc&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; here, but Rust&#x27;s &lt;code&gt;Vec&lt;&#x2F;code&gt; handles the allocation for us, making the code both simpler and more idiomatic. In general, if you see calls to &lt;code&gt;GlobalAlloc&lt;&#x2F;code&gt; when porting some code to Rust, you can probably replace it with a &lt;code&gt;Vec&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
2513&lt;&#x2F;div&gt;
2514&lt;div class=&quot;footnote-definition&quot; id=&quot;4&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;4&lt;&#x2F;sup&gt;
2515&lt;p&gt;This will be a recurring theme.&lt;&#x2F;p&gt;
2516&lt;&#x2F;div&gt;
2517&lt;div class=&quot;footnote-definition&quot; id=&quot;5&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;5&lt;&#x2F;sup&gt;
2518&lt;p&gt;…and similar to &lt;code&gt;EnumProcesses&lt;&#x2F;code&gt;, if the name doesn&#x27;t fit in our buffer, the result will be truncated.&lt;&#x2F;p&gt;
2519&lt;&#x2F;div&gt;
2520</content>
2521	</entry>
2522	<entry xml:lang="en">
2523		<title>Data Mining, Warehousing and Information Retrieval</title>
2524		<published>2020-07-03T00:00:00+00:00</published>
2525		<updated>2020-07-03T00:00:00+00:00</updated>
2526		<link href="https://lonami.dev/blog/university/" type="text/html"/>
2527		<id>https://lonami.dev/blog/university/</id>
2528		<content type="html">&lt;p&gt;During university, there were a few subjects where I had to write blog posts for (either as evaluable tasks or just for fun). I thought it was really fun and I wanted to preserve that work here, with the hopes it&#x27;s interesting to someone.&lt;&#x2F;p&gt;
2529&lt;p&gt;The posts series were auto-generated from the original HTML files and manually anonymized later.&lt;&#x2F;p&gt;
2530&lt;ul&gt;
2531&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;mdad&quot;&gt;Data Mining and Data Warehousing&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
2532&lt;li&gt;&lt;a href=&quot;&#x2F;blog&#x2F;ribw&quot;&gt;Information Retrieval and Web Search&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
2533&lt;&#x2F;ul&gt;
2534</content>
2535	</entry>
2536	<entry xml:lang="en">
2537		<title>My new computer</title>
2538		<published>2020-06-19T00:00:00+00:00</published>
2539		<updated>2020-07-03T00:00:00+00:00</updated>
2540		<link href="https://lonami.dev/blog/new-computer/" type="text/html"/>
2541		<id>https://lonami.dev/blog/new-computer/</id>
2542		<content type="html">&lt;p&gt;This post will be mostly me ranting about setting up a new laptop, but I also just want to share my upgrade. If you&#x27;re considering installing Arch Linux with dual-boot for Windows, maybe this post will help. Or perhaps you will learn something new to troubleshoot systems in the future. Let&#x27;s begin!&lt;&#x2F;p&gt;
2543&lt;p&gt;Last Sunday, I ordered a Asus Rog Strix G531GT-BQ165 for 900€ (on a 20% discount) with the following specifications:&lt;&#x2F;p&gt;
2544&lt;ul&gt;
2545&lt;li&gt;Intel® Core i7-9750H (6 cores, 12MB cache, 2.6GHz up to 4.5GHz, 64-bit)&lt;&#x2F;li&gt;
2546&lt;li&gt;16GB RAM (8GB*2) DDR4 2666MHz&lt;&#x2F;li&gt;
2547&lt;li&gt;512GB SSD M.2 PCIe® NVMe&lt;&#x2F;li&gt;
2548&lt;li&gt;Display 15.6&amp;quot; (1920x1080&#x2F;16:9) 60Hz&lt;&#x2F;li&gt;
2549&lt;li&gt;Graphics NVIDIA® GeForce® GTX1650 4GB GDDR5 VRAM&lt;&#x2F;li&gt;
2550&lt;li&gt;LAN 10&#x2F;100&#x2F;1000&lt;&#x2F;li&gt;
2551&lt;li&gt;Wi-Fi 5 (802.11ac) 2x2 RangeBoost&lt;&#x2F;li&gt;
2552&lt;li&gt;Bluetooth 5.0&lt;&#x2F;li&gt;
2553&lt;li&gt;48Wh battery with 3 cells&lt;&#x2F;li&gt;
2554&lt;li&gt;3 x USB 3.1 (GEN1)&lt;&#x2F;li&gt;
2555&lt;&#x2F;ul&gt;
2556&lt;p&gt;I was mostly interested in a general upgrade (better processor, disk, more RAM), although the graphics card is a really nice addition which will allow me to take some time off on more games. After using it for a bit, I really love the feel of the keyboard, and I love the lack of numpad! (No sarcasm, I really don&#x27;t like numpads.)&lt;&#x2F;p&gt;
2557&lt;p&gt;This is an upgrade from my previous laptop (Asus X554LA-XX822T), which I won in a competition before entering university in a programming challenge. It has served me really well for the past five years, and had the following specifications:&lt;&#x2F;p&gt;
2558&lt;ul&gt;
2559&lt;li&gt;Intel® Core™ i5-5200U&lt;&#x2F;li&gt;
2560&lt;li&gt;4GB RAM DDR3L 1600MHz (which I upgraded to have 8GB)&lt;&#x2F;li&gt;
2561&lt;li&gt;1TB HDD&lt;&#x2F;li&gt;
2562&lt;li&gt;Display 15.6&amp;quot; (1366x768&#x2F;16:9)&lt;&#x2F;li&gt;
2563&lt;li&gt;Intel® HD Graphics 4400&lt;&#x2F;li&gt;
2564&lt;li&gt;LAN 10&#x2F;100&#x2F;1000&lt;&#x2F;li&gt;
2565&lt;li&gt;Wifi 802.11 bgn&lt;&#x2F;li&gt;
2566&lt;li&gt;Bluetooth 4.0&lt;&#x2F;li&gt;
2567&lt;li&gt;Battery 2 cells&lt;&#x2F;li&gt;
2568&lt;li&gt;1 x USB 2.0&lt;&#x2F;li&gt;
2569&lt;li&gt;2 x USB 3.0&lt;&#x2F;li&gt;
2570&lt;&#x2F;ul&gt;
2571&lt;p&gt;Prior to this one, I had a Lenovo (also won in the same competition of the previous year), and prior to that (just for the sake of history), it was HP Pavilion, AMD A4-3300M processor, which unfortunately ended with heating problems. But that&#x27;s very old now.&lt;&#x2F;p&gt;
2572&lt;h2 id=&quot;laptop-arrival&quot;&gt;Laptop arrival&lt;&#x2F;h2&gt;
2573&lt;p&gt;The laptop arrived 2 days ago at roughly 19:00, which I put charged for 3 hours as the book said. The day after, nightmares began!&lt;&#x2F;p&gt;
2574&lt;p&gt;Trying to boot it the first two times was fun, as it comes with a somewhat loud sound on boot. I don&#x27;t know why they would do this, and I immediately turned it off in the BIOS.&lt;&#x2F;p&gt;
2575&lt;h2 id=&quot;installation-journey&quot;&gt;Installation journey&lt;&#x2F;h2&gt;
2576&lt;p&gt;I spent all of yesterday trying to setup Windows and Arch Linux (and didn&#x27;t even finish, it took me this morning too and even now it&#x27;s only half functional). I absolutely &lt;em&gt;hate&lt;&#x2F;em&gt; the amount of partitions the Windows installer creates on a clean disk. So instead, I first went with Arch Linux, and followed the &lt;a href=&quot;https:&#x2F;&#x2F;wiki.archlinux.org&#x2F;index.php&#x2F;Installation_guide&quot;&gt;installation guide on the Arch wiki&lt;&#x2F;a&gt;. Pre-installation, setting up the wireless network, creating the partitions and formatting them went all good. I decided to avoid GRUB at first and go with rEFInd, but alas I missed a big warning on the wiki and after reboot (I would later find out) it was not mounting root properly, so all I had was whatever was in the Initramfs. Reboot didn&#x27;t work, so I had to hold the power button.&lt;&#x2F;p&gt;
2577&lt;p&gt;Anyway, once the partitions were created, I went to install Windows (there was a lot of back and forth burning different &lt;code&gt;.iso&lt;&#x2F;code&gt; images on the USB, which was a bit annoying because it wasn&#x27;t the fastest thing in the world). This was pretty painless, and the process was standard: select advanced to let me choose the right partition, pick the one, say &amp;quot;no&amp;quot; to everything in the services setup, and done. But this was the first Windows &lt;code&gt;.iso&lt;&#x2F;code&gt; I tried. It was an old revision, and the drivers were causing issues when running (something weird about their &lt;code&gt;.dll&lt;&#x2F;code&gt;, manually installing the &lt;code&gt;.ini&lt;&#x2F;code&gt; driver files seemed to work?). The Nvidia drivers didn&#x27;t want to be installed on such an old revision, after updating everything I could via Windows updates. So back I went to burning a newer Windows &lt;code&gt;.iso&lt;&#x2F;code&gt; and going through the same process again…&lt;&#x2F;p&gt;
2578&lt;p&gt;Once Windows was ready and I verified that I could boot to it correctly, it was time to have a second go at Arch Linux. And I went through the setup at least three times, getting it wrong every single time, formatting root every single time, redownloading the packages every single pain. If only had I known earlier what the issue was!&lt;&#x2F;p&gt;
2579&lt;p&gt;Why bother with Arch? I was pretty happy with Linux Mint, and I lowkey wanted to try NixOS, but I had used Arch before and it&#x27;s a really nice distro overall (up-to-date, has AUR, quite minimal, imperative), except for trying to install rEFInd while chrooted…&lt;&#x2F;p&gt;
2580&lt;p&gt;In the end I managed to get something half-working, I still need to properly configure WiFi and pulseaudio in my system but hey it works.&lt;&#x2F;p&gt;
2581&lt;p&gt;I like to be able to dual-boot Windows and Linux because Linux is amazing for productivity, but unfortunately, some games only work fine on Windows. Might as well have both systems and use one for gaming, while the other is my daily driver.&lt;&#x2F;p&gt;
2582&lt;h2 id=&quot;setting-up-arch-linux&quot;&gt;Setting up Arch Linux&lt;&#x2F;h2&gt;
2583&lt;p&gt;This is the process I followed to install Arch Linux in the end, along with a brief explanation on what I think the things are doing and why we are doing them. I think the wiki could do a better job at this, but I also know it&#x27;s hard to get it right for everyone. Something I do dislike is the link colour, after opening a link it becomes gray and it&#x27;s a lot easier to miss the fact that it is a link in the first place, which was tough when re-reading it because some links actually matter a lot. Furthermore, important information may just be a single line, also easy to skim over. Anyway, on to the installation process…&lt;&#x2F;p&gt;
2584&lt;p&gt;The first thing we want to do is configure our keyboard layout or else the keys won&#x27;t correspond to what we expect:&lt;&#x2F;p&gt;
2585&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;loadkeys es
2586&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2587&lt;p&gt;Because we&#x27;re on a recent system, we want to verify that UEFI works correctly. If we see files listed, then it works fine:&lt;&#x2F;p&gt;
2588&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;ls &#x2F;sys&#x2F;firmware&#x2F;efi&#x2F;efivars
2589&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2590&lt;p&gt;The next thing we want to do is configure the WiFi, because I don&#x27;t have any ethernet cable nearby. To do this, we check what network interfaces our laptop has (we&#x27;re looking for the one prefixed with &amp;quot;w&amp;quot;, presumably for wireless, such as &amp;quot;wlan0&amp;quot; or &amp;quot;wlo1&amp;quot;), we set it up, scan for available wireless network, and finally connect. In my case, the network has WPA security so we rely on &lt;code&gt;wpa_supplicant&lt;&#x2F;code&gt; to connect, passing the SSID (network name) and password:&lt;&#x2F;p&gt;
2591&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;ip link
2592ip link set &amp;lt;IFACE&amp;gt; up
2593iw dev &amp;lt;IFACE&amp;gt; scan | less
2594wpa_supplicant -B -i &amp;lt;IFACE&amp;gt; -c &amp;lt;(wpa_passphrase &amp;lt;SSID&amp;gt; &amp;lt;PASS&amp;gt;)
2595&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2596&lt;p&gt;After that&#x27;s done, pinging an IP address like &amp;quot;1.1.1.1&amp;quot; should Just Work™, but to be able to resolve hostnames, we need to also setup a nameserver. I&#x27;m using Cloudflare&#x27;s, but you could use any other:&lt;&#x2F;p&gt;
2597&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;echo nameserver 1.1.1.1 &amp;gt; &#x2F;etc&#x2F;resolv.conf
2598ping archlinux.org
2599^C
2600&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2601&lt;p&gt;If the ping works, then network works! If you still have issues, you may need to &lt;a href=&quot;https:&#x2F;&#x2F;wiki.archlinux.org&#x2F;index.php&#x2F;Network_configuration#Static_IP_address&quot;&gt;manually configure a static IP address&lt;&#x2F;a&gt; and add a route with the address of your, well, router. This basically shows if we have any address, adds a static address (so people know who we are), shows what route we have, and adds a default one (so our packets know where to go):&lt;&#x2F;p&gt;
2602&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;ip address show
2603ip address add &amp;lt;YOUR ADDR&amp;gt;&#x2F;24 broadcast + dev &amp;lt;IFACE&amp;gt;
2604ip route show
2605ip route add default via &amp;lt;ROUTER ADDR&amp;gt; dev &amp;lt;IFACE&amp;gt;
2606&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2607&lt;p&gt;Now that we have network available, we can enable NTP to synchronize our system time (this may be required for network operations where certificates have a validity period, not sure; in any case nobody wants a wrong system time):&lt;&#x2F;p&gt;
2608&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;timedatectl set-ntp true
2609&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2610&lt;p&gt;After that, we can manage our disk and partitions using &lt;code&gt;fdisk&lt;&#x2F;code&gt;. We want to define partitions to tell the system where it should live. To determine the disk name, we first list them, and then edit it. &lt;code&gt;fdisk&lt;&#x2F;code&gt; is really nice and reminds you at every step that help can be accessed with &amp;quot;m&amp;quot;, which you should constantly use to guide you through.&lt;&#x2F;p&gt;
2611&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;fdisk -l
2612fdisk &#x2F;dev&#x2F;&amp;lt;DISK&amp;gt;
2613&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2614&lt;p&gt;The partitions I made are the following:&lt;&#x2F;p&gt;
2615&lt;ul&gt;
2616&lt;li&gt;A 100MB one for the EFI system.&lt;&#x2F;li&gt;
2617&lt;li&gt;A 32GB one for Linux&#x27; root &lt;code&gt;&#x2F;&lt;&#x2F;code&gt; partition.&lt;&#x2F;li&gt;
2618&lt;li&gt;A 200GB one for Linux&#x27; home &lt;code&gt;&#x2F;home&lt;&#x2F;code&gt; partition.&lt;&#x2F;li&gt;
2619&lt;li&gt;The rest was unallocated for Windows because I did this first.&lt;&#x2F;li&gt;
2620&lt;&#x2F;ul&gt;
2621&lt;p&gt;I like to have &lt;code&gt;&#x2F;home&lt;&#x2F;code&gt; and &lt;code&gt;&#x2F;&lt;&#x2F;code&gt; separate because I can reinstall root without losing anything from home (projects, music, photos, screenshots, videos…).&lt;&#x2F;p&gt;
2622&lt;p&gt;After the partitions are made, we format them in FAT32 and EXT4 which are good defaults for EFI, root and home. They need to have a format, or else they won&#x27;t be usable:&lt;&#x2F;p&gt;
2623&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;mkfs.fat -F32 &#x2F;dev&#x2F;&amp;lt;DISK&amp;gt;&amp;lt;PART1&amp;gt;
2624mkfs.ext4 &#x2F;dev&#x2F;&amp;lt;DISK&amp;gt;&amp;lt;PART2&amp;gt;
2625mkfs.ext4 &#x2F;dev&#x2F;&amp;lt;DISK&amp;gt;&amp;lt;PART3&amp;gt;
2626&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2627&lt;p&gt;Because the laptop was new, there was no risk to lose anything, but if you&#x27;re doing a install on a previous system, be very careful with the partition names. Make sure they match with the ones in &lt;code&gt;fdisk -l&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
2628&lt;p&gt;Now that we have usable partitions, we need to mount them or they won&#x27;t be accessible. We can do this with &lt;code&gt;mount&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
2629&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;mount &#x2F;dev&#x2F;&amp;lt;DISK&amp;gt;&amp;lt;PART2&amp;gt; &#x2F;mnt
2630mkdir &#x2F;mnt&#x2F;efi
2631mount &#x2F;dev&#x2F;&amp;lt;DISK&amp;gt;&amp;lt;PART1&amp;gt; &#x2F;mnt&#x2F;efi
2632mkdir &#x2F;mnt&#x2F;home
2633mount &#x2F;dev&#x2F;&amp;lt;DISK&amp;gt;&amp;lt;PART3&amp;gt; &#x2F;mnt&#x2F;home
2634&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2635&lt;p&gt;Remember to use the correct partitions while mounting. We mount everything so that the system knows which partitions we care about, which we will let know about later on.&lt;&#x2F;p&gt;
2636&lt;p&gt;Next step is to setup the basic Arch Linux system on root, which can be done with &lt;code&gt;pacstrap&lt;&#x2F;code&gt;. What follows the directory is a list of packages, and you may choose any you wish (at least add &lt;code&gt;base&lt;&#x2F;code&gt;, &lt;code&gt;linux&lt;&#x2F;code&gt; and &lt;code&gt;linux-firmware&lt;&#x2F;code&gt;). These can be installed later, but I&#x27;d recommend having them from the beginning, just in case:&lt;&#x2F;p&gt;
2637&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;pacstrap &#x2F;mnt base linux linux-firmware sudo vim-minimal dhcpcd wpa_supplicant man-db man-pages intel-ucode grub efibootmgr os-prober ntfs-3g
2638&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2639&lt;p&gt;Because my system has an intel CPU, I also installed &lt;code&gt;intel-ucode&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
2640&lt;p&gt;Next up is generating the &lt;code&gt;fstab&lt;&#x2F;code&gt; file, which we tell to use UUIDs to be on the safe side through &lt;code&gt;-U&lt;&#x2F;code&gt;. This file is important, because without it the system won&#x27;t know what partitions exist and will happily only boot with the initramfs, without anything of what we just installed at root. Not knowing this made me restart the entire installation process a few times.&lt;&#x2F;p&gt;
2641&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;genfstab -U &#x2F;mnt &amp;gt;&amp;gt; &#x2F;mnt&#x2F;etc&#x2F;fstab
2642&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2643&lt;p&gt;After that&#x27;s done, we can change our root into our mount point and finish up configuration. We setup our timezone (so DST can be handled correctly if needed), synchronize the hardware clock (to persist the current time to the BIOS), uncomment our locales (exit &lt;code&gt;vim&lt;&#x2F;code&gt; by pressing ESC, then type &lt;code&gt;:wq&lt;&#x2F;code&gt; and press enter), generate locale files (which some applications need), configure language and keymap, update the hostname of our laptop and what indicate what &lt;code&gt;localhost&lt;&#x2F;code&gt; means…&lt;&#x2F;p&gt;
2644&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;ln -sf &#x2F;usr&#x2F;share&#x2F;zoneinfo&#x2F;&amp;lt;REGION&amp;gt;&#x2F;&amp;lt;CITY&amp;gt; &#x2F;etc&#x2F;localtime
2645hwclock --systohc
2646vim &#x2F;etc&#x2F;locale.gen
2647locale-gen
2648echo LANG=es_ES.UTF-8 &amp;gt; &#x2F;etc&#x2F;locale.conf
2649echo KEYMAP=es &amp;gt; &#x2F;etc&#x2F;vconsole.conf
2650echo &amp;lt;HOST&amp;gt; &#x2F;etc&#x2F;hostname
2651cat &amp;lt;&amp;lt;EOF &amp;gt; &#x2F;etc&#x2F;hosts
2652127.0.0.1 localhost
2653::1 localhost
2654127.0.1.1 &amp;lt;HOST&amp;gt;.localdomain &amp;lt;HOST&amp;gt;
2655EOF
2656&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2657&lt;p&gt;Really, we could&#x27;ve done all of this later, and the same goes for setting root&#x27;s password with &lt;code&gt;passwd&lt;&#x2F;code&gt; or creating users (some of the groups you probably want are &lt;code&gt;power&lt;&#x2F;code&gt; and &lt;code&gt;wheel&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
2658&lt;p&gt;The important part here is installing GRUB (which also needed the &lt;code&gt;efibootmgr&lt;&#x2F;code&gt; package):&lt;&#x2F;p&gt;
2659&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;grub-install --target=x86_64-efi --efi-directory=&#x2F;efi --bootloader-id=GRUB
2660&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2661&lt;p&gt;If we want GRUB to find our Windows install, we also need the &lt;code&gt;os-prober&lt;&#x2F;code&gt; and &lt;code&gt;ntfs-3g&lt;&#x2F;code&gt; packages that we installed earlier with &lt;code&gt;pacstrap&lt;&#x2F;code&gt;, and with those we need to mount the Windows partition somewhere. It doesn&#x27;t matter where. With that done, we can generate the GRUB configuration file which lists all the boot options:&lt;&#x2F;p&gt;
2662&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;mkdir &#x2F;windows
2663mount &#x2F;dev&#x2F;&amp;lt;DISK&amp;gt;&amp;lt;PART5&amp;gt; &#x2F;windows
2664grub-mkconfig -o &#x2F;boot&#x2F;grub&#x2F;grub.cfg
2665&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2666&lt;p&gt;(In my case, I installed Windows before completing the Arch install, which created an additional partition in between).&lt;&#x2F;p&gt;
2667&lt;p&gt;With GRUB ready, we can exit the chroot and reboot the system, and if all went well, you should be greeted with a choice of operating system to use:&lt;&#x2F;p&gt;
2668&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;exit
2669reboot
2670&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2671&lt;p&gt;If for some reason you need to find what mountpoints were active prior to rebooting (to &lt;code&gt;unmount&lt;&#x2F;code&gt; them for example), you can use &lt;code&gt;findmnt&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
2672&lt;p&gt;Before GRUB I tried rEFInd, which as I explained had issues with for missing a warning. Then I tried systemd-boot, which did not pick up Arch at first. That&#x27;s where the several reinstalls come from, I didn&#x27;t want to work with a half-worked system so I mostly redid the entire process quite a few times.&lt;&#x2F;p&gt;
2673&lt;h2 id=&quot;migrating-to-the-new-laptop&quot;&gt;Migrating to the new laptop&lt;&#x2F;h2&gt;
2674&lt;p&gt;I had a external disk formatted with NTFS. Of course, after moving every file I cared about from my previous Linux install caused all the permissions to reset. All my &lt;code&gt;.git&lt;&#x2F;code&gt; repositories, dirty with file permission changes! This is going to take a while to fix, or maybe I should just &lt;code&gt;git config core.fileMode false&lt;&#x2F;code&gt;. Here is a &lt;a href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;2083563&quot;&gt;lovely command&lt;&#x2F;a&gt; to sort them out on a per-repository basis:&lt;&#x2F;p&gt;
2675&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;git diff --summary | grep --color &#x27;mode change 100644 =&amp;gt; 100755&#x27; | cut -d&#x27; &#x27; -f7- | xargs -d&#x27;\n&#x27; chmod -x
2676&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2677&lt;p&gt;I never realized how much I had stored over the years, but it really was a lot. While moving things to the external disk, I tried to do some cleanup, such as removing some build artifacts which needlessly occupy space, or completely skipping all the binary application files. If I need those I will install them anyway. The process was mostly focused on finding all the projects and program data that I did care about, or even some game saves. Nothing too difficult, but definitely time consuming.&lt;&#x2F;p&gt;
2678&lt;h2 id=&quot;tuning-arch&quot;&gt;Tuning Arch&lt;&#x2F;h2&gt;
2679&lt;p&gt;Now that our system is ready, install &lt;code&gt;pacman-contrib&lt;&#x2F;code&gt; to grab a copy of the &lt;code&gt;rankmirrors&lt;&#x2F;code&gt; speed. It should help speed up the download of whatever packages you want to install, since it will help us &lt;a href=&quot;https:&#x2F;&#x2F;wiki.archlinux.org&#x2F;index.php&#x2F;Mirrors#List_by_speed&quot;&gt;rank the mirrors by download speed&lt;&#x2F;a&gt;. Making a copy of the file is important, otherwise whenever you try to install something it will fail saying it can&#x27;t find anything.&lt;&#x2F;p&gt;
2680&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;cp &#x2F;etc&#x2F;pacman.d&#x2F;mirrorlist &#x2F;etc&#x2F;pacman.d&#x2F;mirrorlist.backup
2681sed -i &#x27;s&#x2F;^#Server&#x2F;Server&#x2F;&#x27; &#x2F;etc&#x2F;pacman.d&#x2F;mirrorlist.backup
2682rankmirrors -n 6 &#x2F;etc&#x2F;pacman.d&#x2F;mirrorlist.backup | tee &#x2F;etc&#x2F;pacman.d&#x2F;mirrorlist
2683&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2684&lt;p&gt;This will take a while, but it should be well worth it. We&#x27;re using &lt;code&gt;tee&lt;&#x2F;code&gt; to see the progress as it goes.&lt;&#x2F;p&gt;
2685&lt;p&gt;Some other packages I installed after I had a working system in no particular order:&lt;&#x2F;p&gt;
2686&lt;ul&gt;
2687&lt;li&gt;&lt;code&gt;xfce4&lt;&#x2F;code&gt; and &lt;code&gt;xorg-server&lt;&#x2F;code&gt;. I just love the simplicity of XFCE.&lt;&#x2F;li&gt;
2688&lt;li&gt;&lt;code&gt;xfce4-whiskermenu-plugin&lt;&#x2F;code&gt;, a really nice start menu.&lt;&#x2F;li&gt;
2689&lt;li&gt;&lt;code&gt;xfce4-pulseaudio-plugin&lt;&#x2F;code&gt; and &lt;code&gt;pavucontrol&lt;&#x2F;code&gt;, to quickly adjust the audio with my mouse.&lt;&#x2F;li&gt;
2690&lt;li&gt;&lt;code&gt;xfce4-taskmanager&lt;&#x2F;code&gt;, a GUI alternative I generally prefer to &lt;code&gt;htop&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
2691&lt;li&gt;&lt;code&gt;pulseaudio&lt;&#x2F;code&gt; and &lt;code&gt;pulseaudio-alsa&lt;&#x2F;code&gt; to get nice integration with XFCE4 and audio mixing.&lt;&#x2F;li&gt;
2692&lt;li&gt;&lt;code&gt;firefox&lt;&#x2F;code&gt;, which comes with fonts too. A really good web browser.&lt;&#x2F;li&gt;
2693&lt;li&gt;&lt;code&gt;git&lt;&#x2F;code&gt;, to commit &lt;del&gt;crimes&lt;&#x2F;del&gt; code.&lt;&#x2F;li&gt;
2694&lt;li&gt;&lt;code&gt;code&lt;&#x2F;code&gt;, a wonderful editor which I used to write this blog entry.&lt;&#x2F;li&gt;
2695&lt;li&gt;&lt;code&gt;nano&lt;&#x2F;code&gt;, so much nicer to write a simple commit message.&lt;&#x2F;li&gt;
2696&lt;li&gt;&lt;code&gt;python&lt;&#x2F;code&gt; and &lt;code&gt;python-pip&lt;&#x2F;code&gt;, my favourite language to toy around ideas or use as a calculator.&lt;&#x2F;li&gt;
2697&lt;li&gt;&lt;code&gt;telegram-desktop&lt;&#x2F;code&gt;, for my needs on sharing memes.&lt;&#x2F;li&gt;
2698&lt;li&gt;&lt;code&gt;cmus&lt;&#x2F;code&gt; and &lt;code&gt;mpv&lt;&#x2F;code&gt;, a simple terminal music player and media player.&lt;&#x2F;li&gt;
2699&lt;li&gt;&lt;code&gt;openssh&lt;&#x2F;code&gt;, to connect into any VPS I have access to.&lt;&#x2F;li&gt;
2700&lt;li&gt;&lt;code&gt;base-devel&lt;&#x2F;code&gt;, necessary to build most projects I&#x27;ll find myself working with (or even compiling some projects Rust which I installed via &lt;code&gt;rustup&lt;&#x2F;code&gt;).&lt;&#x2F;li&gt;
2701&lt;li&gt;&lt;code&gt;flac&lt;&#x2F;code&gt;, &lt;code&gt;libmad&lt;&#x2F;code&gt;, &lt;code&gt;opus&lt;&#x2F;code&gt;, and &lt;code&gt;libvorbis&lt;&#x2F;code&gt;, to be able to play more audio files.&lt;&#x2F;li&gt;
2702&lt;li&gt;&lt;code&gt;inkscape&lt;&#x2F;code&gt;, to make random drawings.&lt;&#x2F;li&gt;
2703&lt;li&gt;&lt;code&gt;ffmpeg&lt;&#x2F;code&gt;, to convert media or record screen.&lt;&#x2F;li&gt;
2704&lt;li&gt;&lt;code&gt;xclip&lt;&#x2F;code&gt;, to automatically copy screenshots to my clipboard.&lt;&#x2F;li&gt;
2705&lt;li&gt;&lt;code&gt;gvfs&lt;&#x2F;code&gt;, needed by Thunar to handle mounting and having a trash (perma-deletion by default can be nasty sometimes).&lt;&#x2F;li&gt;
2706&lt;li&gt;&lt;code&gt;noto-fonts&lt;&#x2F;code&gt;, &lt;code&gt;noto-fonts-cjk&lt;&#x2F;code&gt;, &lt;code&gt;noto-fonts-extra&lt;&#x2F;code&gt; and &lt;code&gt;noto-fonts-emoji&lt;&#x2F;code&gt;, if you don&#x27;t want missing gliphs everywhere.&lt;&#x2F;li&gt;
2707&lt;li&gt;&lt;code&gt;xfce4-notifyd&lt;&#x2F;code&gt; and &lt;code&gt;libnotify&lt;&#x2F;code&gt;, for notifications.&lt;&#x2F;li&gt;
2708&lt;li&gt;&lt;code&gt;cronie&lt;&#x2F;code&gt;, to be able to &lt;code&gt;crontab -e&lt;&#x2F;code&gt;. Make sure to &lt;code&gt;system enable cronie&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
2709&lt;li&gt;&lt;code&gt;xarchiver&lt;&#x2F;code&gt; (with &lt;code&gt;p7zip&lt;&#x2F;code&gt;, &lt;code&gt;zip&lt;&#x2F;code&gt;, &lt;code&gt;unzip&lt;&#x2F;code&gt; and &lt;code&gt;unrar&lt;&#x2F;code&gt;) to uncompress stuff.&lt;&#x2F;li&gt;
2710&lt;li&gt;&lt;code&gt;xreader&lt;&#x2F;code&gt; to read &lt;code&gt;.pdf&lt;&#x2F;code&gt; files.&lt;&#x2F;li&gt;
2711&lt;li&gt;&lt;code&gt;sqlitebrowser&lt;&#x2F;code&gt; is always nice to tinker around with SQLite databases.&lt;&#x2F;li&gt;
2712&lt;li&gt;&lt;code&gt;jre8-openjdk&lt;&#x2F;code&gt; if you want to run Java applications.&lt;&#x2F;li&gt;
2713&lt;li&gt;&lt;code&gt;smartmontools&lt;&#x2F;code&gt; is nice with a SSD to view your disk statistics.&lt;&#x2F;li&gt;
2714&lt;&#x2F;ul&gt;
2715&lt;p&gt;After that, I configured my Super L key to launch &lt;code&gt;xfce4-popup-whiskermenu&lt;&#x2F;code&gt; so that it opens the application menu, pretty much the same as it would on Windows, moved the panels around and configured them to my needs, and it feels like home once more.&lt;&#x2F;p&gt;
2716&lt;p&gt;I made some mistakes while &lt;a href=&quot;https:&#x2F;&#x2F;wiki.archlinux.org&#x2F;index.php&#x2F;Systemd-networkd&quot;&gt;configuring systemd-networkd&lt;&#x2F;a&gt; and accidentally added a service that was incorrect, which caused boot to wait for it to timeout before completing. My boot time was taking 90 seconds longer because of this! &lt;a href=&quot;https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;archlinux&#x2F;comments&#x2F;4nv9yi&#x2F;my_arch_greets_me_now_with_a_start_job&#x2F;&quot;&gt;The solution was to remove said service&lt;&#x2F;a&gt;, so this is something to look out for.&lt;&#x2F;p&gt;
2717&lt;p&gt;In order to find what was taking long, I had to edit the &lt;a href=&quot;https:&#x2F;&#x2F;wiki.archlinux.org&#x2F;index.php&#x2F;kernel_parameters&quot;&gt;kernel parameters&lt;&#x2F;a&gt; to remove the &lt;code&gt;quiet&lt;&#x2F;code&gt; option. I prefer seeing the output on what my computer is doing anyway, because it gives me a sense of progress and most importantly is of great value when things go wrong. Another interesting option is &lt;code&gt;noauto,x-systemd.automount&lt;&#x2F;code&gt;, which makes a disk lazily-mounted. If you have a slow disk, this could help speed things up.&lt;&#x2F;p&gt;
2718&lt;p&gt;If you see a service taking long, you can also use &lt;code&gt;systemd-analyze blame&lt;&#x2F;code&gt; to see what takes the longest, and &lt;code&gt;systemctl list-dependencies&lt;&#x2F;code&gt; is also helpful to find what services are active.&lt;&#x2F;p&gt;
2719&lt;p&gt;My &lt;code&gt;locale charmap&lt;&#x2F;code&gt; was spitting out a bunch of warnings:&lt;&#x2F;p&gt;
2720&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;$ locale charmap
2721locale: Cannot set LC_CTYPE to default locale: No such file or directory
2722locale: Cannot set LC_MESSAGES to default locale: No such file or directory
2723locale: Cannot set LC_ALL to default locale: No such file or directory
2724ANSI_X3.4-1968
2725&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2726&lt;p&gt;…ANSI encoding? Immediately I added the following to &lt;code&gt;~&#x2F;.bashrc&lt;&#x2F;code&gt; and &lt;code&gt;~&#x2F;.profile&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
2727&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;export LC_ALL=en_US.UTF-8
2728export LANG=en_US.UTF-8
2729export LANGUAGE=en_US.UTF-8
2730&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2731&lt;p&gt;For some reason, I also had to edit &lt;code&gt;xfce4-terminal&lt;&#x2F;code&gt;&#x27;s preferences in advanced to change the default character encoding to UTF-8. This also solved my issues with pasting things into the terminal, and also proper rendering! I guess pastes were not working because it had some characters that could not be encoded.&lt;&#x2F;p&gt;
2732&lt;p&gt;To have working notifications, I added the following to &lt;code&gt;~&#x2F;.bash_profile&lt;&#x2F;code&gt; after &lt;code&gt;exec startx&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
2733&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;systemctl --user start xfce4-notifyd.service
2734&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2735&lt;p&gt;I&#x27;m pretty sure there&#x27;s a better way to do this, or maybe it&#x27;s not even necessary, but this works for me.&lt;&#x2F;p&gt;
2736&lt;p&gt;Some of the other things I had left to do was setting up &lt;code&gt;sccache&lt;&#x2F;code&gt; to speed up Rust builds:&lt;&#x2F;p&gt;
2737&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;cargo install sccache
2738echo export RUSTC_WRAPPER=sccache &amp;gt;&amp;gt; ~&#x2F;.bashrc
2739&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2740&lt;p&gt;Once I had &lt;code&gt;cargo&lt;&#x2F;code&gt; ready, installed &lt;code&gt;hacksaw&lt;&#x2F;code&gt; and &lt;code&gt;shotgun&lt;&#x2F;code&gt; with it to perform screenshots.&lt;&#x2F;p&gt;
2741&lt;p&gt;I also disabled the security delay when downloading files in Firefox because it&#x27;s just annoying, in &lt;code&gt;about:config&lt;&#x2F;code&gt; setting &lt;code&gt;security.dialog_enable_delay&lt;&#x2F;code&gt; to &lt;code&gt;0&lt;&#x2F;code&gt;, and added the &lt;a href=&quot;https:&#x2F;&#x2F;alisdair.mcdiarmid.org&#x2F;kill-sticky-headers&#x2F;&quot;&gt;Kill sticky headers&lt;&#x2F;a&gt; to my bookmarks (you may prefer &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;t-mart&#x2F;kill-sticky&quot;&gt;the updated version&lt;&#x2F;a&gt;).&lt;&#x2F;p&gt;
2742&lt;p&gt;The &lt;code&gt;utils-linux&lt;&#x2F;code&gt; comes with a &lt;code&gt;fstrim&lt;&#x2F;code&gt; utility to &lt;a href=&quot;https:&#x2F;&#x2F;wiki.archlinux.org&#x2F;index.php&#x2F;Solid_state_drive#Periodic_TRIM&quot;&gt;trim the SSD weekly&lt;&#x2F;a&gt;, which I want enabled via &lt;code&gt;systemctl enable fstrim.timer&lt;&#x2F;code&gt; (you may also want to &lt;code&gt;start&lt;&#x2F;code&gt; it if you don&#x27;t reboot often). For more SSD tips, check &lt;a href=&quot;https:&#x2F;&#x2F;easylinuxtipsproject.blogspot.com&#x2F;p&#x2F;ssd.html&quot;&gt;How to optimize your Solid State Drive&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
2743&lt;p&gt;If the sound is funky prior to reboot, try &lt;code&gt;pulseaudio --kill&lt;&#x2F;code&gt; and &lt;code&gt;pulseaudio --start&lt;&#x2F;code&gt;, or delete &lt;code&gt;~&#x2F;.config&#x2F;pulse&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
2744&lt;p&gt;I haven&#x27;t been able to get the brightness keys to work yet, but it&#x27;s not a big deal, because scrolling on the power manager plugin of Xfce does work (and also &lt;code&gt;xbacklight&lt;&#x2F;code&gt; works, or writing directly to &lt;code&gt;&#x2F;sys&#x2F;class&#x2F;backlight&#x2F;*&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
2745&lt;h2 id=&quot;tuning-windows&quot;&gt;Tuning Windows&lt;&#x2F;h2&gt;
2746&lt;p&gt;On the Windows side, I disabled the annoying Windows defender by running (&lt;kbd&gt;Ctrl+R&lt;&#x2F;kbd&gt;) &lt;code&gt;gpedit.msc&lt;&#x2F;code&gt; and editing:&lt;&#x2F;p&gt;
2747&lt;ul&gt;
2748&lt;li&gt;&lt;em&gt;Computer Configuration &amp;gt; Administrative Templates &amp;gt; Windows Components &amp;gt; Windows Defender » Turn off Windows Defender » Enable&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
2749&lt;li&gt;&lt;em&gt;User Configuration &amp;gt; Administrative Templates &amp;gt; Start Menu and Taskbar » Remove Notifications and Action Center » Enable&lt;&#x2F;em&gt;&lt;&#x2F;li&gt;
2750&lt;&#x2F;ul&gt;
2751&lt;p&gt;I also updated the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;WindowsLies&#x2F;BlockWindows&#x2F;raw&#x2F;master&#x2F;hosts&quot;&gt;&lt;code&gt;hosts&lt;&#x2F;code&gt; file&lt;&#x2F;a&gt; (located at &lt;code&gt;%windir%\system32\Drivers\etc\hosts&lt;&#x2F;code&gt;) with the hope that it will stop some of the telemetry.&lt;&#x2F;p&gt;
2752&lt;p&gt;Last, to have consistent time on Windows and Linux, I changed the following registry key for a &lt;code&gt;qword&lt;&#x2F;code&gt; with value &lt;code&gt;1&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
2753&lt;pre&gt;&lt;code&gt;HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation\RealTimeIsUniversal
2754&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2755&lt;p&gt;(The key might not exist, but you can create it if that&#x27;s the case).&lt;&#x2F;p&gt;
2756&lt;p&gt;All this time, my laptop had the keyboard lights on, which have been quite annoying. Apparently, they also can cause &lt;a href=&quot;https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;ValveIndex&#x2F;comments&#x2F;cm6pos&#x2F;psa_uninstalldisable_aura_sync_lighting_if_you&#x2F;&quot;&gt;massive FPS drops&lt;&#x2F;a&gt;. I headed over to &lt;a href=&quot;https:&#x2F;&#x2F;rog.asus.com&#x2F;downloads&#x2F;&quot;&gt;Asus Rog downloads&lt;&#x2F;a&gt;, selected Aura Sync…&lt;&#x2F;p&gt;
2757&lt;pre&gt;&lt;code class=&quot;language-md&quot; data-lang=&quot;md&quot;&gt;# Not Found
2758
2759The requested URL &#x2F;campaign&#x2F;aura&#x2F;us&#x2F;Sync.html was not found on this server.
2760
2761Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
2762&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2763&lt;p&gt;…great! I&#x27;ll just find the &lt;a href=&quot;https:&#x2F;&#x2F;www.asus.com&#x2F;campaign&#x2F;aura&#x2F;global&#x2F;&quot;&gt;Aura site&lt;&#x2F;a&gt; somewhere else…&lt;&#x2F;p&gt;
2764&lt;pre&gt;&lt;code class=&quot;language-md&quot; data-lang=&quot;md&quot;&gt;# ASUS
2765
2766# We&#x27;ll be back.
2767
2768Hi, our website is temporarily closed for service enhancements.
2769
2770We&#x27;ll be back shortly.Thank you for your patience!
2771&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2772&lt;p&gt;Oh come on. After waiting for the next day, I headed over, downloaded their software, tried to install it and it was an awful experience. It felt like I was purposedly installing malware. It spammed and flashed a lot of &lt;code&gt;cmd&lt;&#x2F;code&gt;&#x27;s on screen as if it was a virus. It was stuck at 100% doing that and then, Windows blue-screened with &lt;code&gt;KERNEL_MODE_HEAP_CORRUPTION&lt;&#x2F;code&gt;. Amazing. How do you screw up this bad?&lt;&#x2F;p&gt;
2773&lt;p&gt;Well, at least rebooting worked. I tried to &lt;a href=&quot;https:&#x2F;&#x2F;answers.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;forum&#x2F;all&#x2F;unable-to-uninstall-asus-aura-sync-utility&#x2F;e9bec36c-e62f-4773-80be-88fb68dace16&quot;&gt;uninstall Aura, but of course that failed&lt;&#x2F;a&gt;. Using the &lt;a href=&quot;https:&#x2F;&#x2F;support.microsoft.com&#x2F;en-us&#x2F;help&#x2F;17588&#x2F;windows-fix-problems-that-block-programs-being-installed-or-removed&quot;&gt;troubleshooter to uninstall programs&lt;&#x2F;a&gt; helped me remove most of the crap that was installed.&lt;&#x2F;p&gt;
2774&lt;p&gt;After searching around how to disable the lights (because &lt;a href=&quot;https:&#x2F;&#x2F;rog.asus.com&#x2F;forum&#x2F;showthread.php?112786-Option-to-Disable-Aura-Lights-on-Strix-G-series-(G531GT)-irrespective-of-OSes&quot;&gt;my BIOS did not have this setting&lt;&#x2F;a&gt;), I stumbled upon &lt;a href=&quot;https:&#x2F;&#x2F;rog.asus.com&#x2F;us&#x2F;innovation&#x2F;armoury_crate&#x2F;&quot;&gt;&amp;quot;Armoury Crate&amp;quot;&lt;&#x2F;a&gt;. Okay, fine, I will install that.&lt;&#x2F;p&gt;
2775&lt;p&gt;The experience wasn&#x27;t much better. It did the same thing with a lot of consoles flashing on screen. And of course, it resulted in another blue-screen, this time &lt;code&gt;KERNEL_SECURITY_CHECK_FAILURE&lt;&#x2F;code&gt;. To finish up, the BSOD kept happening as I rebooted the system. &lt;del&gt;Time to reinstall Windows once more.&lt;&#x2F;del&gt; After booting and crashing a few more times I could get into secure mode and perform the reinstall from there, which saved me from burning the &lt;code&gt;.iso&lt;&#x2F;code&gt; again.&lt;&#x2F;p&gt;
2776&lt;p&gt;Asus software might be good, but the software is utter crap.&lt;&#x2F;p&gt;
2777&lt;p&gt;After trying out &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;wroberts&#x2F;rogauracore&quot;&gt;rogauracore&lt;&#x2F;a&gt; (which didn&#x27;t list my model), it worked! I could disable the stupid lights from Linux, and &lt;a href=&quot;https:&#x2F;&#x2F;gitlab.com&#x2F;CalcProgrammer1&#x2F;OpenRGB&#x2F;-&#x2F;wikis&#x2F;home&quot;&gt;OpenRGB&lt;&#x2F;a&gt; also works on Windows which may be worth checking out too.&lt;&#x2F;p&gt;
2778&lt;p&gt;Because &lt;code&gt;rougauracore&lt;&#x2F;code&gt; helped me and they linked to &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;linuxhw&#x2F;hw-probe&#x2F;blob&#x2F;master&#x2F;README.md#appimage&quot;&gt;hw-probe&lt;&#x2F;a&gt;, I decided to &lt;a href=&quot;https:&#x2F;&#x2F;linux-hardware.org&#x2F;?probe=0e3e48c501&quot;&gt;run it on my system&lt;&#x2F;a&gt;, with the hopes it is useful for other people.&lt;&#x2F;p&gt;
2779&lt;h2 id=&quot;closing-words&quot;&gt;Closing words&lt;&#x2F;h2&gt;
2780&lt;p&gt;I hope the installation journey is at least useful to someone, or that you enjoyed reading about it all. If not, sorry!&lt;&#x2F;p&gt;
2781</content>
2782	</entry>
2783	<entry xml:lang="en">
2784		<title>Tips for Outpost</title>
2785		<published>2020-05-10T00:00:00+00:00</published>
2786		<updated>2020-05-22T00:00:00+00:00</updated>
2787		<link href="https://lonami.dev/blog/tips-outpost/" type="text/html"/>
2788		<id>https://lonami.dev/blog/tips-outpost/</id>
2789		<content type="html">&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;store.steampowered.com&#x2F;app&#x2F;1127110&#x2F;Outpost&#x2F;&quot;&gt;Outpost&lt;&#x2F;a&gt; is a fun little game by Open Mid Interactive that has popped in recently in my recommended section of Steam, and I decided to give it a try.&lt;&#x2F;p&gt;
2790&lt;p&gt;It&#x27;s a fun tower-defense game with progression, different graphics and random world generation which makes it quite fun for a few hours. In this post I want to talk about some tips I found useful to get past night 50.&lt;&#x2F;p&gt;
2791&lt;h2 id=&quot;build-pattern&quot;&gt;Build Pattern&lt;&#x2F;h2&gt;
2792&lt;p&gt;At first, you may be inclined to design a checkerboard pattern like the following, where &amp;quot;C&amp;quot; is the Crystal shrine, &amp;quot;S&amp;quot; is a stone launcher and &amp;quot;B&amp;quot; is a booster:&lt;&#x2F;p&gt;
2793&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;tips-outpost&#x2F;outpost-bad-pattern.svg&quot; alt=&quot;Bad Outpost build pattern&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
2794&lt;p&gt;Indeed, this pattern will apply &lt;strong&gt;4&lt;&#x2F;strong&gt; boosts to every turret, but unfortunately, the other 4 slots of the booster are wasted! This is because boosters are able to power 8 different towers, and you really want to maximize that. Here&#x27;s a better design:&lt;&#x2F;p&gt;
2795&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;tips-outpost&#x2F;outpost-good-pattern.svg&quot; alt=&quot;Good Outpost build pattern&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
2796&lt;p&gt;The shrine&#x27;s tower does get boosted, but it&#x27;s still not really worth it to boost it. This pattern works good, and it&#x27;s really easy to tile: just repeat the same 3x3 pattern.&lt;&#x2F;p&gt;
2797&lt;p&gt;Nonetheless, we can do better. What if we applied multiple boosters to the same tower while still applying all 8 boosts?&lt;&#x2F;p&gt;
2798&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;tips-outpost&#x2F;outpost-best-pattern.svg&quot; alt=&quot;Best Outpost build pattern&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
2799&lt;p&gt;That&#x27;s what peak performance looks like. You can actually apply multiple boosters to the same tower, and it works great.&lt;&#x2F;p&gt;
2800&lt;p&gt;Now, is it really worth it building anywhere except around the shrine? Not really. You never know where a boss will come from, so all sides need a lot of defense if you want to stand a chance.&lt;&#x2F;p&gt;
2801&lt;p&gt;The addition of traps in 1.6 is amazing. You want to build these outside your strong &amp;quot;core&amp;quot;, mostly to slow the enemies down so your turrets have more time to finish them off. Don&#x27;t waste boosters on the traps, and build them at a reasonable distance from the center (the sixth tile is a good spot):&lt;&#x2F;p&gt;
2802&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;tips-outpost&#x2F;outpost-trap-pattern.svg&quot; alt=&quot;Trap Outpost build pattern&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
2803&lt;p&gt;If you gather enough materials, you can build more trap and cannon layers outside, roughly at enough distance to slow them for enough duration until they reach the next layer of traps, and so on. Probably a single gap of &amp;quot;cannon, booster, cannon&amp;quot; is enough between trap layers, just not in the center where you need a lot of fire power.&lt;&#x2F;p&gt;
2804&lt;h2 id=&quot;talents&quot;&gt;Talents&lt;&#x2F;h2&gt;
2805&lt;p&gt;Talents are the way progression works in the game. Generally, after a run, you will have enough experience to upgrade nearly all talents of roughly the same tier. However, some are worth upgrading more than others (which provide basically no value).&lt;&#x2F;p&gt;
2806&lt;p&gt;The best ones to upgrade are:&lt;&#x2F;p&gt;
2807&lt;ul&gt;
2808&lt;li&gt;Starting supplies. Amazing to get good tools early.&lt;&#x2F;li&gt;
2809&lt;li&gt;Shrine shield. Very useful to hold against tough bosses.&lt;&#x2F;li&gt;
2810&lt;li&gt;Better buildings (cannon, boosters, bed and traps). They&#x27;re a must to deal the most damage.&lt;&#x2F;li&gt;
2811&lt;li&gt;Better pickaxe. Stone is limited, so better make good use of it.&lt;&#x2F;li&gt;
2812&lt;li&gt;Better chests. They provide an insane amount of resources early.&lt;&#x2F;li&gt;
2813&lt;li&gt;Winter slow. Turrets will have more time to deal damage, it&#x27;s perfect.&lt;&#x2F;li&gt;
2814&lt;li&gt;More time. Useful if you&#x27;re running out, although generally you enter nights early after having a good core anyway.&lt;&#x2F;li&gt;
2815&lt;li&gt;More rocks. Similar to a better pickaxe, more stone is always better.&lt;&#x2F;li&gt;
2816&lt;&#x2F;ul&gt;
2817&lt;p&gt;Some decent ones:&lt;&#x2F;p&gt;
2818&lt;ul&gt;
2819&lt;li&gt;In-shrine turret. It&#x27;s okay to get past the first night without building but not much beyond that.&lt;&#x2F;li&gt;
2820&lt;li&gt;Better axe and greaves. Great to save some energy and really nice quality of life to move around.&lt;&#x2F;li&gt;
2821&lt;li&gt;Tree growth. Normally there&#x27;s enough trees for this not to be an issue but it can save some time gathering wood.&lt;&#x2F;li&gt;
2822&lt;li&gt;Wisps. They&#x27;re half-decent since they can provide materials once you max out or max out expensive gear.&lt;&#x2F;li&gt;
2823&lt;&#x2F;ul&gt;
2824&lt;p&gt;Some okay ones:&lt;&#x2F;p&gt;
2825&lt;ul&gt;
2826&lt;li&gt;Extra XP while playing. Generally not needed due to the way XP scales per night, but can be a good boost.&lt;&#x2F;li&gt;
2827&lt;li&gt;Runestones. Not as reliable as chests but some can grant more energy per day.&lt;&#x2F;li&gt;
2828&lt;&#x2F;ul&gt;
2829&lt;p&gt;Some crap ones:&lt;&#x2F;p&gt;
2830&lt;ul&gt;
2831&lt;li&gt;Boosts for other seasons. I mean, winter is already the best, no use there.&lt;&#x2F;li&gt;
2832&lt;li&gt;Bow. The bow is very useless at the moment, it&#x27;s not worth your experience.&lt;&#x2F;li&gt;
2833&lt;li&gt;More energy per bush. Not really worth hunting for bushes since you will have enough energy to do well.&lt;&#x2F;li&gt;
2834&lt;&#x2F;ul&gt;
2835&lt;h2 id=&quot;turrets&quot;&gt;Turrets&lt;&#x2F;h2&gt;
2836&lt;p&gt;Always build the highest tier, there&#x27;s no point in anything lower than that. You will need to deal a lot of damage in a small area, which means space is a premium.&lt;&#x2F;p&gt;
2837&lt;h2 id=&quot;boosters&quot;&gt;Boosters&lt;&#x2F;h2&gt;
2838&lt;p&gt;If you&#x27;re very early in the game, I recommend alternating both the flag and torch in a checkerboard pattern where the boosters should go in the pattern above. This way your towers will get extra speed and extra range, which works great.&lt;&#x2F;p&gt;
2839&lt;p&gt;When you&#x27;re in mid-game (stone launchers, gears and campfires), I do not recommend using campfires. The issue is their range boost is way too long, and the turrets will miss quite a few shots. It&#x27;s better to put all your power into fire speed for increased DPS, at least near the center. If you manage to build too far out and some of the turrets hardly ever shoot, you may put campfires there.&lt;&#x2F;p&gt;
2840&lt;p&gt;In end-game, of course alternate both of the highest tier upgrades. They are really good, and provide the best benefit &#x2F; cost ratio.&lt;&#x2F;p&gt;
2841&lt;h2 id=&quot;gathering-materials&quot;&gt;Gathering Materials&lt;&#x2F;h2&gt;
2842&lt;p&gt;It is &lt;strong&gt;very&lt;&#x2F;strong&gt; important to use all your energy every day! Otherwise it will go to waste, and you will need a lot of materials.&lt;&#x2F;p&gt;
2843&lt;p&gt;As of 1.6, you can mine two things at once if they&#x27;re close enough! I don&#x27;t know if this is intended or a bug, but it sure is great.&lt;&#x2F;p&gt;
2844&lt;p&gt;Once you&#x27;re in mid-game, your stone-based fort should stand pretty well against the nights on its own. After playing for a while you will notice, if your base can defend a boss, then it will have no issue carrying you through the nights until the next boss. You can (and should!) spend the nights gathering materials, but only when you&#x27;re confident that the night won&#x27;t run out.&lt;&#x2F;p&gt;
2845&lt;p&gt;Before the boss hits (every fifth night), come back to your base and use all of your materials. This is the next fort upgrade that will carry it the five next nights.&lt;&#x2F;p&gt;
2846&lt;p&gt;You may also speed up time during night, but make sure you use all your energy before hand. And also take care, in the current version of the game speeding up time only speeds up monster movement, not the fire rate or projectile speed of your turrets! This means they will miss more shots and can be pretty dangerous. If you&#x27;re speeding up time, consider speeding it up for a little bit, then go back to normal until things are more calm, and repeat.&lt;&#x2F;p&gt;
2847&lt;p&gt;If you&#x27;re in the end-game, try to rush for chests. They provide a huge amount of materials which is really helpful to upgrade all your tools early so you can make sure to get the most out of every rock left in the map.&lt;&#x2F;p&gt;
2848&lt;p&gt;In the end-game, after all stone has been collected, you don&#x27;t really need to use all of your energy anymore. Just enough to have enough wood to build with the remaining stone. This will also be nice with the bow upgrades, which admitedly can get quite powerful, but it&#x27;s best to have a strong fort first.&lt;&#x2F;p&gt;
2849&lt;h2 id=&quot;season&quot;&gt;Season&lt;&#x2F;h2&gt;
2850&lt;p&gt;In my opinion, winter is just the best of the seasons. You don&#x27;t &lt;em&gt;really&lt;&#x2F;em&gt; need that much energy (it gets tiresome), or extra tree drops, or luck. Slower movement means your turrets will be able to shoot enemies for longer, dealing more damage over time, giving them more chance to take enemies out before they reach the shrine.&lt;&#x2F;p&gt;
2851&lt;p&gt;Feel free to re-roll the map a few times (play and exit, or even restart the game) until you get winter if you want to go for The Play.&lt;&#x2F;p&gt;
2852&lt;h2 id=&quot;gear&quot;&gt;Gear&lt;&#x2F;h2&gt;
2853&lt;p&gt;In my opinion, you really should rush for the best pickaxe you can afford. Stone is a limited resource that doesn&#x27;t regrow like trees, so once you run out, it&#x27;s over. Better to make the best use out of it with a good pickaxe!&lt;&#x2F;p&gt;
2854&lt;p&gt;You may also upgrade your greaves, we all known faster movement is a &lt;em&gt;really&lt;&#x2F;em&gt; nice quality of life improvement.&lt;&#x2F;p&gt;
2855&lt;p&gt;Of course, you will eventually upgrade your axe to chop wood (otherwise it&#x27;s wasted energy, really), but it&#x27;s not as much of a priority as the pickaxe.&lt;&#x2F;p&gt;
2856&lt;p&gt;Now, the bow is completely useless. Don&#x27;t bother with it. Your energy is better spent gathering materials to build permanent turrets that deal constant damage while you&#x27;re away, and the damage adds up with every extra turret you build.&lt;&#x2F;p&gt;
2857&lt;p&gt;With regards to items you carry (like sword, or helmet), look for these (from best to worst):&lt;&#x2F;p&gt;
2858&lt;ul&gt;
2859&lt;li&gt;Less minion life.&lt;&#x2F;li&gt;
2860&lt;li&gt;Chance to not consume energy.&lt;&#x2F;li&gt;
2861&lt;li&gt;+1 turret damage.&lt;&#x2F;li&gt;
2862&lt;li&gt;Extra energy.&lt;&#x2F;li&gt;
2863&lt;li&gt;+1 drop from trees or stones.&lt;&#x2F;li&gt;
2864&lt;li&gt;+1 free wood or stone per day.&lt;&#x2F;li&gt;
2865&lt;&#x2F;ul&gt;
2866&lt;p&gt;Less minion life, nothing to say. You will need it near end-game.&lt;&#x2F;p&gt;
2867&lt;p&gt;The chance to not consume energy is better the more energy you have. With a 25% chance not to consume energy, you can think of it as 1 extra energy for every 4 energy you have on average.&lt;&#x2F;p&gt;
2868&lt;p&gt;Turret damage is a tough one, it&#x27;s &lt;em&gt;amazing&lt;&#x2F;em&gt; mid-game (it basically doubles your damage) but falls short once you unlock the cannon where you may prefer other items. Definitely recommended if you&#x27;re getting started. You may even try to roll it on low tiers by dying on the second night, because it&#x27;s that good.&lt;&#x2F;p&gt;
2869&lt;p&gt;Extra energy is really good, because it means you can get more materials before it gets too rough. Make sure you have built at least two beds in the first night! This extra energy will pay of for the many nights to come.&lt;&#x2F;p&gt;
2870&lt;p&gt;The problem with free wood or stone per day is that you have, often, five times as much energy per day. By this I mean you can get easily 5 stone every day, which means 5 extra stone, whereas the other would provide just 1 per night. On a good run, you will get around 50 free stone or 250 extra stone. It&#x27;s a clear winner.&lt;&#x2F;p&gt;
2871&lt;p&gt;In end-game, more quality of life are revealing chests so that you can rush them early, if you like to hunt for them try to make better use of the slot.&lt;&#x2F;p&gt;
2872&lt;h2 id=&quot;closing-words&quot;&gt;Closing words&lt;&#x2F;h2&gt;
2873&lt;p&gt;I hope you enjoy the game as much as I do! Movement is sometimes janky and there&#x27;s the occassional lag spikes, but despite this it should provide at least a few good hours of gameplay. Beware however a good run can take up to an hour!&lt;&#x2F;p&gt;
2874</content>
2875	</entry>
2876	<entry xml:lang="en">
2877		<title>Python ctypes and Windows</title>
2878		<published>2019-06-19T00:00:00+00:00</published>
2879		<updated>2019-06-19T00:00:00+00:00</updated>
2880		<link href="https://lonami.dev/blog/ctypes-and-windows/" type="text/html"/>
2881		<id>https://lonami.dev/blog/ctypes-and-windows/</id>
2882		<content type="html">&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.python.org&#x2F;&quot;&gt;Python&lt;&#x2F;a&gt;&#x27;s &lt;a href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;ctypes.html&quot;&gt;&lt;code&gt;ctypes&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; is quite a nice library to easily load and invoke C methods available in already-compiled &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Dynamic-link_library&quot;&gt;&lt;code&gt;.dll&lt;&#x2F;code&gt; files&lt;&#x2F;a&gt; without any additional dependencies. And I &lt;em&gt;love&lt;&#x2F;em&gt; depending on as little as possible.&lt;&#x2F;p&gt;
2883&lt;p&gt;In this blog post, we will walk through my endeavors to use &lt;code&gt;ctypes&lt;&#x2F;code&gt; with the &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;api&#x2F;&quot;&gt;Windows API&lt;&#x2F;a&gt;, and do some cool stuff with it.&lt;&#x2F;p&gt;
2884&lt;p&gt;We will assume some knowledge of C&#x2F;++ and Python, since we will need to read and write a bit of both. Please note that this post is only an introduction to &lt;code&gt;ctypes&lt;&#x2F;code&gt;, and if you need more information you should consult the &lt;a href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;ctypes.html&quot;&gt;Python&#x27;s documentation for &lt;code&gt;ctypes&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
2885&lt;p&gt;While the post focuses on Windows&#x27; API, the code here probably applies to unix-based systems with little modifications.&lt;&#x2F;p&gt;
2886&lt;h2 id=&quot;basics&quot;&gt;Basics&lt;&#x2F;h2&gt;
2887&lt;p&gt;First of all, let&#x27;s learn how to load a library. Let&#x27;s say we want to load &lt;code&gt;User32.dll&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
2888&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;import ctypes
2889
2890ctypes.windll.user32
2891&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2892&lt;p&gt;Yes, it&#x27;s that simple. When you access an attribute of &lt;code&gt;windll&lt;&#x2F;code&gt;, said library will load. Since Windows is case-insensitive, we will use lowercase consistently.&lt;&#x2F;p&gt;
2893&lt;p&gt;Calling a function is just as simple. Let&#x27;s say you want to call &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;api&#x2F;winuser&#x2F;nf-winuser-setcursorpos&quot;&gt;&lt;code&gt;SetCursorPos&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, which is defined as follows:&lt;&#x2F;p&gt;
2894&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;BOOL SetCursorPos(
2895    int X,
2896    int Y
2897);
2898&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2899&lt;p&gt;Okay, it returns a &lt;code&gt;bool&lt;&#x2F;code&gt; and takes two inputs, &lt;code&gt;x&lt;&#x2F;code&gt; and &lt;code&gt;y&lt;&#x2F;code&gt;. So we can call it like so:&lt;&#x2F;p&gt;
2900&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;ctypes.windll.user32.SetCursorPos(100, 100)
2901&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2902&lt;p&gt;Try it! Your cursor will move!&lt;&#x2F;p&gt;
2903&lt;h2 id=&quot;funky-stuff&quot;&gt;Funky Stuff&lt;&#x2F;h2&gt;
2904&lt;p&gt;We can go a bit more crazy and make it form a spiral:&lt;&#x2F;p&gt;
2905&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;import math
2906import time
2907
2908for i in range(200):
2909    x = int(500 + math.cos(i &#x2F; 5) * i)
2910    y = int(500 + math.sin(i &#x2F; 5) * i)
2911    ctypes.windll.user32.SetCursorPos(x, y)
2912    time.sleep(0.05)
2913&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2914&lt;p&gt;Ah, it&#x27;s always so pleasant to do random stuff when programming. Sure makes it more fun.&lt;&#x2F;p&gt;
2915&lt;h2 id=&quot;complex-structures&quot;&gt;Complex Structures&lt;&#x2F;h2&gt;
2916&lt;p&gt;&lt;code&gt;SetCursorPos&lt;&#x2F;code&gt; was really simple. It took two parameters and they both were integers. Let&#x27;s go with something harder. Let&#x27;s go with &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;api&#x2F;winuser&#x2F;nf-winuser-sendinput&quot;&gt;&lt;code&gt;SendInput&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;! Emulating input will be a fun exercise:&lt;&#x2F;p&gt;
2917&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;UINT SendInput(
2918    UINT    cInputs,
2919    LPINPUT pInputs,
2920    int     cbSize
2921);
2922&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2923&lt;p&gt;Okay, &lt;code&gt;LPINPUT&lt;&#x2F;code&gt;, what are you? Microsoft likes to prefix types with what they are. In this case, &lt;code&gt;LP&lt;&#x2F;code&gt; stands for &amp;quot;Long Pointer&amp;quot; (I guess?), so &lt;code&gt;LPINPUT&lt;&#x2F;code&gt; is just a Long Pointer to &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;api&#x2F;winuser&#x2F;ns-winuser-taginput&quot;&gt;&lt;code&gt;INPUT&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
2924&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;typedef struct tagINPUT {
2925    DWORD type;
2926    union {
2927        MOUSEINPUT    mi;
2928        KEYBDINPUT    ki;
2929        HARDWAREINPUT hi;
2930    } DUMMYUNIONNAME;
2931} INPUT, *PINPUT, *LPINPUT;
2932&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2933&lt;p&gt;Alright, that&#x27;s new. We have a &lt;code&gt;struct&lt;&#x2F;code&gt; and &lt;code&gt;union&lt;&#x2F;code&gt;, two different concepts. We can define both with &lt;code&gt;ctypes&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
2934&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;INPUT_MOUSE = 0
2935INPUT_KEYBOARD = 1
2936INPUT_HARDWARE = 2
2937
2938class INPUT(ctypes.Structure):
2939    _fields_ = [
2940        (&#x27;type&#x27;, ctypes.c_long),
2941        ...
2942    ]
2943&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2944&lt;p&gt;Structures are classes that subclass &lt;code&gt;ctypes.Structure&lt;&#x2F;code&gt;, and you define their fields in the &lt;code&gt;_fields_&lt;&#x2F;code&gt; class-level variable, which is a list of tuples &lt;code&gt;(field name, field type)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
2945&lt;p&gt;The C structure had a &lt;code&gt;DWORD type&lt;&#x2F;code&gt;. &lt;code&gt;DWORD&lt;&#x2F;code&gt; is a &lt;code&gt;c_long&lt;&#x2F;code&gt;, and &lt;code&gt;type&lt;&#x2F;code&gt; is a name like any other, which is why we did &lt;code&gt;(&#x27;type&#x27;, ctypes.c_long)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
2946&lt;p&gt;But what about the union? It&#x27;s anonymous, and we can&#x27;t make anonymous unions (&lt;em&gt;citation needed&lt;&#x2F;em&gt;) with &lt;code&gt;ctypes&lt;&#x2F;code&gt;. We will give it a concrete name and a type.&lt;&#x2F;p&gt;
2947&lt;p&gt;Before defining the union, we need to define its inner structures, &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;api&#x2F;winuser&#x2F;ns-winuser-tagmouseinput&quot;&gt;&lt;code&gt;MOUSEINPUT&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;api&#x2F;winuser&#x2F;ns-winuser-tagkeybdinput&quot;&gt;&lt;code&gt;KEYBDINPUT&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;api&#x2F;winuser&#x2F;ns-winuser-taghardwareinput&quot;&gt;&lt;code&gt;HARDWAREINPUT&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. We won&#x27;t be using them all, but since they count towards the final struct size (C will choose the largest structure as the final size), we need them, or Windows&#x27; API will get confused and refuse to work (personal experience):&lt;&#x2F;p&gt;
2948&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;class MOUSEINPUT(ctypes.Structure):
2949    _fields_ = [
2950        (&#x27;dx&#x27;, ctypes.c_long),
2951        (&#x27;dy&#x27;, ctypes.c_long),
2952        (&#x27;mouseData&#x27;, ctypes.c_long),
2953        (&#x27;dwFlags&#x27;, ctypes.c_long),
2954        (&#x27;time&#x27;, ctypes.c_long),
2955        (&#x27;dwExtraInfo&#x27;, ctypes.POINTER(ctypes.c_ulong))
2956    ]
2957
2958
2959class KEYBDINPUT(ctypes.Structure):
2960    _fields_ = [
2961        (&#x27;wVk&#x27;, ctypes.c_short),
2962        (&#x27;wScan&#x27;, ctypes.c_short),
2963        (&#x27;dwFlags&#x27;, ctypes.c_long),
2964        (&#x27;time&#x27;, ctypes.c_long),
2965        (&#x27;dwExtraInfo&#x27;, ctypes.POINTER(ctypes.c_ulong))
2966    ]
2967
2968
2969class HARDWAREINPUT(ctypes.Structure):
2970    _fields_ = [
2971        (&#x27;uMsg&#x27;, ctypes.c_long),
2972        (&#x27;wParamL&#x27;, ctypes.c_short),
2973        (&#x27;wParamH&#x27;, ctypes.c_short)
2974    ]
2975
2976
2977class INPUTUNION(ctypes.Union):
2978    _fields_ = [
2979        (&#x27;mi&#x27;, MOUSEINPUT),
2980        (&#x27;ki&#x27;, KEYBDINPUT),
2981        (&#x27;hi&#x27;, HARDWAREINPUT)
2982    ]
2983
2984
2985class INPUT(ctypes.Structure):
2986    _fields_ = [
2987        (&#x27;type&#x27;, ctypes.c_long),
2988        (&#x27;value&#x27;, INPUTUNION)
2989    ]
2990&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
2991&lt;p&gt;Some things to note:&lt;&#x2F;p&gt;
2992&lt;ul&gt;
2993&lt;li&gt;Pointers are defined as &lt;code&gt;ctypes.POINTER(inner type)&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
2994&lt;li&gt;The field names can be anything you want. You can make them more &amp;quot;pythonic&amp;quot; if you want (such as changing &lt;code&gt;dwExtraInfo&lt;&#x2F;code&gt; for just &lt;code&gt;extra_info&lt;&#x2F;code&gt;), but I chose to stick with the original naming.&lt;&#x2F;li&gt;
2995&lt;li&gt;The union is very similar, but it uses &lt;code&gt;ctypes.Union&lt;&#x2F;code&gt; instead of &lt;code&gt;ctypes.Structure&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
2996&lt;li&gt;We gave a name to the anonymous union, &lt;code&gt;INPUTUNION&lt;&#x2F;code&gt;, and used it inside &lt;code&gt;INPUT&lt;&#x2F;code&gt; with also a made-up name, &lt;code&gt;(&#x27;value&#x27;, INPUTUNION)&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
2997&lt;&#x2F;ul&gt;
2998&lt;p&gt;Now that we have all the types we need defined, we can use them:&lt;&#x2F;p&gt;
2999&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;KEYEVENTF_KEYUP = 0x0002
3000
3001def press(vk, down):
3002    inputs = INPUT(type=INPUT_KEYBOARD, value=INPUTUNION(ki=KEYBDINPUT(
3003        wVk=vk,
3004        wScan=0,
3005        dwFlags=0 if down else KEYEVENTF_KEYUP,
3006        time=0,
3007        dwExtraInfo=None
3008    )))
3009    ctypes.windll.user32.SendInput(1, ctypes.byref(inputs), ctypes.sizeof(inputs))
3010
3011
3012for char in &#x27;HELLO&#x27;:
3013    press(ord(char), down=True)
3014    press(ord(char), down=False)
3015&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3016&lt;p&gt;Run it! It will press and release the keys &lt;code&gt;hello&lt;&#x2F;code&gt; to type the word &lt;code&gt;&amp;quot;hello&amp;quot;&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
3017&lt;p&gt;&lt;code&gt;vk&lt;&#x2F;code&gt; stands for &amp;quot;virtual key&amp;quot;. Letters correspond with their upper-case ASCII value, which is what we did above. You can find all the available keys in the page with all the &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;inputdev&#x2F;virtual-key-codes&quot;&gt;Virtual Key Codes&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
3018&lt;h2 id=&quot;dynamic-inputs-and-pointers&quot;&gt;Dynamic Inputs and Pointers&lt;&#x2F;h2&gt;
3019&lt;p&gt;What happens if a method wants something by reference? That is, a pointer to your thing? For example, &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;api&#x2F;winuser&#x2F;nf-winuser-getcursorpos&quot;&gt;&lt;code&gt;GetCursorPos&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
3020&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;typedef struct tagPOINT {
3021    LONG x;
3022    LONG y;
3023} POINT, *PPOINT, *NPPOINT, *LPPOINT;
3024
3025BOOL GetCursorPos(
3026    LPPOINT lpPoint
3027);
3028&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3029&lt;p&gt;It wants a Long Pointer to &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;desktop&#x2F;api&#x2F;windef&#x2F;ns-windef-point&quot;&gt;&lt;code&gt;POINT&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. We can do just that with &lt;code&gt;ctypes.byref&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
3030&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;class POINT(ctypes.Structure):
3031    _fields_ = [
3032        (&#x27;x&#x27;, ctypes.c_long),
3033        (&#x27;y&#x27;, ctypes.c_long)
3034    ]
3035
3036
3037def get_mouse():
3038    point = POINT()
3039    ctypes.windll.user32.GetCursorPos(ctypes.byref(point))
3040    #                  pass our point by ref ^^^^^
3041    # this lets GetCursorPos fill its x and y fields
3042
3043    return point.x, point.y
3044
3045
3046while True:
3047    print(get_mouse())
3048    time.sleep(0.05)
3049&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3050&lt;p&gt;Now you can track the mouse position! Make sure to &lt;code&gt;Ctrl+C&lt;&#x2F;code&gt; the program when you&#x27;re tired of it.&lt;&#x2F;p&gt;
3051&lt;p&gt;What happens if a method wants a dynamically-sized input?&lt;&#x2F;p&gt;
3052&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;buffer = ctypes.create_string_buffer(size)
3053&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3054&lt;p&gt;In that case, you can create an in-memory &lt;code&gt;buffer&lt;&#x2F;code&gt; of &lt;code&gt;size&lt;&#x2F;code&gt; with &lt;code&gt;ctypes.create_string_buffer&lt;&#x2F;code&gt;. It will return a character array of that size, which you can pass as a pointer directly (without &lt;code&gt;ctypes.byref&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
3055&lt;p&gt;To access the buffer&#x27;s contents, you can use either &lt;code&gt;.raw&lt;&#x2F;code&gt; or &lt;code&gt;.value&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
3056&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;entire_buffer_as_bytes = buffer.raw
3057up_until_null = buffer.value
3058&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3059&lt;p&gt;When the method fills in the data, you can &lt;code&gt;cast&lt;&#x2F;code&gt; your buffer back into a pointer of a concrete type:&lt;&#x2F;p&gt;
3060&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;result_ptr = ctypes.cast(buffer, ctypes.POINTER(ctypes.c_long))
3061&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3062&lt;p&gt;And you can de-reference pointers with &lt;code&gt;.contents&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
3063&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;first_result = result_ptr.contents
3064&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3065&lt;h2 id=&quot;arrays&quot;&gt;Arrays&lt;&#x2F;h2&gt;
3066&lt;p&gt;Arrays are defined as &lt;code&gt;type * size&lt;&#x2F;code&gt;. Your linter may not like that, and if you don&#x27;t know the size beforehand, consider creating a 0-sized array. For example:&lt;&#x2F;p&gt;
3067&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;# 10 longs
3068ten_longs = (ctypes.c_long * 10)()
3069for i in range(10):
3070    ten_longs[i] = 2 ** i
3071
3072# Unknown size of longs, e.g. inside some Structure
3073longs = (ctypes.c_long * 0)
3074
3075# Now you know how many longs it actually was
3076known_longs = ctypes.cast(
3077    ctypes.byref(longs),
3078    ctypes.POINTER(ctypes.c_long * size)
3079).contents
3080&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3081&lt;p&gt;If there&#x27;s a better way to initialize arrays, please let me know.&lt;&#x2F;p&gt;
3082&lt;h2 id=&quot;wintypes&quot;&gt;wintypes&lt;&#x2F;h2&gt;
3083&lt;p&gt;Under Windows, the &lt;code&gt;ctypes&lt;&#x2F;code&gt; module has a &lt;code&gt;wintypes&lt;&#x2F;code&gt; submodule. This one contains definitions like &lt;code&gt;HWND&lt;&#x2F;code&gt; which may be useful and can be imported as:&lt;&#x2F;p&gt;
3084&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;from ctypes.wintypes import HWND, LPCWSTR, UINT
3085&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3086&lt;h2 id=&quot;callbacks&quot;&gt;Callbacks&lt;&#x2F;h2&gt;
3087&lt;p&gt;Some functions (I&#x27;m looking at you, &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows&#x2F;win32&#x2F;api&#x2F;winuser&#x2F;nf-winuser-enumwindows&quot;&gt;&lt;code&gt;EnumWindows&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;) ask us to pass a callback. In this case, it wants a &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;previous-versions&#x2F;windows&#x2F;desktop&#x2F;legacy&#x2F;ms633498(v=vs.85)&quot;&gt;&lt;code&gt;EnumWindowsProc&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
3088&lt;pre&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;BOOL EnumWindows(
3089    WNDENUMPROC lpEnumFunc,
3090    LPARAM      lParam
3091);
3092
3093BOOL CALLBACK EnumWindowsProc(
3094    _In_ HWND   hwnd,
3095    _In_ LPARAM lParam
3096);
3097&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3098&lt;p&gt;The naive approach won&#x27;t work:&lt;&#x2F;p&gt;
3099&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;def callback(hwnd, lParam):
3100    print(hwnd)
3101    return True
3102
3103ctypes.windll.user32.EnumWindows(callback, 0)
3104# ctypes.ArgumentError: argument 1: &amp;lt;class &#x27;TypeError&#x27;&amp;gt;: Don&#x27;t know how to convert parameter 1
3105# Aww.
3106&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3107&lt;p&gt;Instead, you must wrap your function as a C definition like so:&lt;&#x2F;p&gt;
3108&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;from ctypes.wintypes import BOOL, HWND, LPARAM
3109
3110EnumWindowsProc = ctypes.WINFUNCTYPE(BOOL, HWND, LPARAM)
3111
3112def callback(hwnd, lParam):
3113    print(hwnd)
3114    return True
3115
3116# Wrap the function in the C definition
3117callback = EnumWindowsProc(callback)
3118
3119ctypes.windll.user32.EnumWindows(callback, 0)
3120# Yay, it works.
3121&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3122&lt;p&gt;You may have noticed this is what decorators do, wrap the function. So…&lt;&#x2F;p&gt;
3123&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;from ctypes.wintypes import BOOL, HWND, LPARAM
3124
3125@ctypes.WINFUNCTYPE(BOOL, HWND, LPARAM)
3126def callback(hwnd, lParam):
3127    print(hwnd)
3128    return True
3129
3130ctypes.windll.user32.EnumWindows(callback, 0)
3131&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3132&lt;p&gt;…will also work. And it is a &lt;em&gt;lot&lt;&#x2F;em&gt; fancier.&lt;&#x2F;p&gt;
3133&lt;h2 id=&quot;closing-words&quot;&gt;Closing Words&lt;&#x2F;h2&gt;
3134&lt;p&gt;With the knowledge above and some experimentation, you should be able to call and do (almost) anything you want. That was pretty much all I needed on my project anyway :)&lt;&#x2F;p&gt;
3135&lt;p&gt;We have been letting Python convert Python values into C values, but you can do so explicitly too. For example, you can use &lt;code&gt;ctypes.c_short(17)&lt;&#x2F;code&gt; to make sure to pass that &lt;code&gt;17&lt;&#x2F;code&gt; as a &lt;code&gt;short&lt;&#x2F;code&gt;. And if you have a &lt;code&gt;c_short&lt;&#x2F;code&gt;, you can convert or cast it to its Python &lt;code&gt;.value&lt;&#x2F;code&gt; as &lt;code&gt;some_short.value&lt;&#x2F;code&gt;. The same applies for integers, longs, floats, doubles… pretty much anything, char pointers (strings) included.&lt;&#x2F;p&gt;
3136&lt;p&gt;If you can&#x27;t find something in their online documentation, you can always &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;BurntSushi&#x2F;ripgrep&quot;&gt;&lt;code&gt;rg&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; for it in the &lt;code&gt;C:\Program Files (x86)\Windows Kits\10\Include\*&lt;&#x2F;code&gt; directory.&lt;&#x2F;p&gt;
3137&lt;p&gt;Note that the &lt;code&gt;ctypes.Structure&lt;&#x2F;code&gt;&#x27;s that you define can have more methods of your own. For example, you can write them a &lt;code&gt;__str__&lt;&#x2F;code&gt; to easily view its fields, or define a &lt;code&gt;@property&lt;&#x2F;code&gt; to re-interpret some data in a meaningful way.&lt;&#x2F;p&gt;
3138&lt;p&gt;For enumerations, you can pass just the right integer number, make a constant for it, or if you prefer, use a &lt;a href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;enum.html#enum.IntEnum&quot;&gt;&lt;code&gt;enum.IntEnum&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;. For example, &lt;a href=&quot;https:&#x2F;&#x2F;docs.microsoft.com&#x2F;en-us&#x2F;windows-hardware&#x2F;manufacture&#x2F;desktop&#x2F;dism&#x2F;dismloglevel-enumeration&quot;&gt;&lt;code&gt;DismLogLevel&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; would be:&lt;&#x2F;p&gt;
3139&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;class DismLogLevel(enum.IntEnum):
3140    DismLogErrors = 0
3141    DismLogErrorsWarnings = 1
3142    DismLogErrorsWarningsInfo = 2
3143&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3144&lt;p&gt;And you &lt;em&gt;should&lt;&#x2F;em&gt; be able to pass &lt;code&gt;DismLogLevel.DismLogErrors&lt;&#x2F;code&gt; as the parameter now.&lt;&#x2F;p&gt;
3145&lt;p&gt;If you see a function definition like &lt;code&gt;Function(void)&lt;&#x2F;code&gt;, that&#x27;s C&#x27;s way of saying it takes no parameters, so just call it as &lt;code&gt;Function()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3146&lt;p&gt;Make sure to pass all parameters, even if they seem optional they probably still want a &lt;code&gt;NULL&lt;&#x2F;code&gt; at least, and of course, read the documentation well. Some methods have certain pre-conditions.&lt;&#x2F;p&gt;
3147&lt;p&gt;Have fun hacking!&lt;&#x2F;p&gt;
3148</content>
3149	</entry>
3150	<entry xml:lang="en">
3151		<title>Shattered Pixel Dungeon</title>
3152		<published>2019-06-03T00:00:00+00:00</published>
3153		<updated>2019-06-03T00:00:00+00:00</updated>
3154		<link href="https://lonami.dev/blog/pixel-dungeon/" type="text/html"/>
3155		<id>https://lonami.dev/blog/pixel-dungeon/</id>
3156		<content type="html">&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;shatteredpixel.com&#x2F;shatteredpd&#x2F;&quot;&gt;Shattered Pixel Dungeon&lt;&#x2F;a&gt; is the classic roguelike RPG game with randomly-generated dungeons. As a new player, it was a bit frustrating to be constantly killed on the first levels of the dungeon, but with some practice it&#x27;s easy to reach high levels if you can kill the first boss.&lt;&#x2F;p&gt;
3157&lt;h2 id=&quot;basic-tips&quot;&gt;Basic Tips&lt;&#x2F;h2&gt;
3158&lt;p&gt;The game comes with its own tips, but here&#x27;s a short and straight-forward summary:&lt;&#x2F;p&gt;
3159&lt;ul&gt;
3160&lt;li&gt;&lt;strong&gt;Don&#x27;t rush into enemies&lt;&#x2F;strong&gt;. Abuse doors and small corridors to kill them one by one. You can use the clock on the bottom left to wait a turn without moving.&lt;&#x2F;li&gt;
3161&lt;li&gt;&lt;strong&gt;Explore each level at full&lt;&#x2F;strong&gt;. You will find goodies and gain XP while doing so.&lt;&#x2F;li&gt;
3162&lt;li&gt;&lt;strong&gt;Upon finding a special room&lt;&#x2F;strong&gt; (e.g. has a chest but is protected by piranhas), drink all potions that you found in that level until there&#x27;s one that helps you (e.g. be invisible so piranhas leave you alone). There is guaranteed to be a helpful one per level with special rooms.&lt;&#x2F;li&gt;
3163&lt;li&gt;&lt;strong&gt;Drink potions as early as possible&lt;&#x2F;strong&gt;. Harmful potions do less damage on early levels (and if you die, you lose less). This will keep them identified early for the rest of the game.&lt;&#x2F;li&gt;
3164&lt;li&gt;&lt;strong&gt;Read scrolls as early as possible&lt;&#x2F;strong&gt; as well. This will keep them identified. It may be worth to wait until you have an item which may be cursed and until the level is clear, because some scrolls clean curses and others alert enemies.&lt;&#x2F;li&gt;
3165&lt;li&gt;&lt;strong&gt;Food and health are resources&lt;&#x2F;strong&gt; that you have to &lt;em&gt;manage&lt;&#x2F;em&gt;, not keep them always at full. Even if you are starving and taking damage, you may not need to eat &lt;em&gt;just yet&lt;&#x2F;em&gt;, since food is scarce. Eat when you are low on health or in possible danger.&lt;&#x2F;li&gt;
3166&lt;li&gt;&lt;strong&gt;Piranhas&lt;&#x2F;strong&gt;. Seriously, just leave them alone if you are melee. They&#x27;re free food if you&#x27;re playing ranged, though.&lt;&#x2F;li&gt;
3167&lt;li&gt;&lt;strong&gt;Prefer armor over weapons&lt;&#x2F;strong&gt;. And make sure to identify or clean it from curses before wearing anything!&lt;&#x2F;li&gt;
3168&lt;li&gt;&lt;strong&gt;Find a dew vial early&lt;&#x2F;strong&gt;. It&#x27;s often a better idea to store dew (health) for later than to use it as soon as possible.&lt;&#x2F;li&gt;
3169&lt;&#x2F;ul&gt;
3170&lt;h2 id=&quot;bosses&quot;&gt;Bosses&lt;&#x2F;h2&gt;
3171&lt;p&gt;There is a boss every 5 levels.&lt;&#x2F;p&gt;
3172&lt;ul&gt;
3173&lt;li&gt;&lt;strong&gt;Level 5 boss&lt;&#x2F;strong&gt;. Try to stay on water, but don&#x27;t let &lt;em&gt;it&lt;&#x2F;em&gt; stay on water since it will heal. Be careful when he starts enraging.&lt;&#x2F;li&gt;
3174&lt;li&gt;&lt;strong&gt;Level 10 boss&lt;&#x2F;strong&gt;. Ranged weapons are good against it.&lt;&#x2F;li&gt;
3175&lt;li&gt;&lt;strong&gt;Level 15 boss&lt;&#x2F;strong&gt;. I somehow managed to tank it with a health potion.&lt;&#x2F;li&gt;
3176&lt;li&gt;&lt;strong&gt;Level 20 boss&lt;&#x2F;strong&gt;. I didn&#x27;t get this far just yet. You are advised to use scrolls of magic mapping in the last levels to skip straight to the boss, since there&#x27;s nothing else of value.&lt;&#x2F;li&gt;
3177&lt;li&gt;&lt;strong&gt;Level 25 boss&lt;&#x2F;strong&gt;. The final boss. Good job if you made it this far!&lt;&#x2F;li&gt;
3178&lt;&#x2F;ul&gt;
3179&lt;h2 id=&quot;mage&quot;&gt;Mage&lt;&#x2F;h2&gt;
3180&lt;p&gt;If you followed the basic tips, you will sooner or later make use of two scrolls of upgrade in a single run. This will unlock the mage class, which is ridiculously powerful. He starts with a ranged-weapon, a magic missile wand, which is really helpful to keep enemies at a distance. Normally, you want to use this at first to surprise attack them soon, and if you are low on charges, you may go melee on normal enemies if you are confident.&lt;&#x2F;p&gt;
3181&lt;h2 id=&quot;luck&quot;&gt;Luck&lt;&#x2F;h2&gt;
3182&lt;p&gt;This game is all about luck and patience! Some runs will be better than others, and you should thank and pray the RNG gods for them. If you don&#x27;t, they will only give you cursed items and not a single scroll to clean them. So, good luck and enjoy playing!&lt;&#x2F;p&gt;
3183</content>
3184	</entry>
3185	<entry xml:lang="en">
3186		<title>Installing NixOS, Take 2</title>
3187		<published>2019-02-15T00:00:00+00:00</published>
3188		<updated>2019-02-16T00:00:00+00:00</updated>
3189		<link href="https://lonami.dev/blog/installing-nixos-2/" type="text/html"/>
3190		<id>https://lonami.dev/blog/installing-nixos-2/</id>
3191		<content type="html">&lt;p&gt;This is my second take at installing NixOS, after a while being frustrated with Arch Linux and the fact that a few kernel upgrades ago, the system crashed randomly from time to time. &lt;code&gt;journalctl&lt;&#x2F;code&gt; did not have any helpful hints and I thought reinstalling could be worthwhile anyway.&lt;&#x2F;p&gt;
3192&lt;p&gt;This time, I started with more knowledge! The first step is heading to the &lt;a href=&quot;https:&#x2F;&#x2F;nixos.org&quot;&gt;NixOS website&lt;&#x2F;a&gt; and downloading their minimal installation CD for 64 bits. I didn&#x27;t go with their graphical live CD, because their &lt;a href=&quot;https:&#x2F;&#x2F;nixos.org&#x2F;nixos&#x2F;manual&quot;&gt;installation manual&lt;&#x2F;a&gt; is a wonderful resource that guides you nicely.&lt;&#x2F;p&gt;
3193&lt;p&gt;Once you have downloaded their &lt;code&gt;.iso&lt;&#x2F;code&gt;, you should probably verify it&#x27;s &lt;code&gt;sha256sum&lt;&#x2F;code&gt; and make sure that it matches. The easiest thing to do in my opinion is using an USB to burn the image in it. Plug it in and check its device name with &lt;code&gt;fdisk -l&lt;&#x2F;code&gt;. In my case, it was &lt;code&gt;&#x2F;dev&#x2F;sdb&lt;&#x2F;code&gt;, so I went ahead with it and ran &lt;code&gt;dd if=nixos.iso of=&#x2F;dev&#x2F;sdb status=progress&lt;&#x2F;code&gt;. Make sure to run &lt;code&gt;sync&lt;&#x2F;code&gt; once that&#x27;s done.&lt;&#x2F;p&gt;
3194&lt;p&gt;If either &lt;code&gt;dd&lt;&#x2F;code&gt; or &lt;code&gt;sync&lt;&#x2F;code&gt; seem &amp;quot;stuck&amp;quot; in the end, they are just flushing the changes to disk to make sure all is good. This is normal, and depends on your drives.&lt;&#x2F;p&gt;
3195&lt;p&gt;Now, reboot your computer with the USB plugged in and make sure to boot into it. You should be welcome with a pretty screen. Just select the first option and wait until it logs you in as root. Once you&#x27;re there you probably want to &lt;code&gt;loadkeys es&lt;&#x2F;code&gt; or whatever your keyboard layout is, or you will have a hard time with passwords, since the characters are all over the place.&lt;&#x2F;p&gt;
3196&lt;p&gt;In a clean disk, you would normally create the partitions now. In my case, I already had the partitions made (100MB for the EFI system, where &lt;code&gt;&#x2F;boot&lt;&#x2F;code&gt; lives, 40GB for the root &lt;code&gt;&#x2F;&lt;&#x2F;code&gt; partition with my old Linux installation, and 700G for &lt;code&gt;&#x2F;home&lt;&#x2F;code&gt;), so I didn&#x27;t need to do anything here. The manual showcases &lt;code&gt;parted&lt;&#x2F;code&gt;, but I personally use &lt;code&gt;fdisk&lt;&#x2F;code&gt;, which has very helpful help I check every time I use it.&lt;&#x2F;p&gt;
3197&lt;p&gt;&lt;strong&gt;Important&lt;&#x2F;strong&gt;: The &lt;code&gt;XY&lt;&#x2F;code&gt; in &lt;code&gt;&#x2F;dev&#x2F;sdXY&lt;&#x2F;code&gt; is probably different in your system! Make sure you use &lt;code&gt;fdisk -l&lt;&#x2F;code&gt; to see the correct letters and numbers!&lt;&#x2F;p&gt;
3198&lt;p&gt;With the partitions ready in my UEFI system, I formatted both &lt;code&gt;&#x2F;&lt;&#x2F;code&gt; and &lt;code&gt;&#x2F;boot&lt;&#x2F;code&gt; just to be safe with &lt;code&gt;mkfs.ext4 -L nixos &#x2F;dev&#x2F;sda2&lt;&#x2F;code&gt; and &lt;code&gt;mkfs.fat -F 32 -n boot &#x2F;dev&#x2F;sda1&lt;&#x2F;code&gt; (remember that these are the letters and numbers used in my partition scheme). Don&#x27;t worry about the warning in the second command regarding lowercase letters and Windows. It&#x27;s not really an issue.&lt;&#x2F;p&gt;
3199&lt;p&gt;Now, since we gave each partition a label, we can easily mount them through &lt;code&gt;mount &#x2F;dev&#x2F;disk&#x2F;by-label&#x2F;nixos &#x2F;mnt&lt;&#x2F;code&gt; and, in UEFI systems, be sure to &lt;code&gt;mkdir -p &#x2F;mnt&#x2F;boot&lt;&#x2F;code&gt; and &lt;code&gt;mount &#x2F;dev&#x2F;disk&#x2F;by-label&#x2F;boot &#x2F;mnt&#x2F;boot&lt;&#x2F;code&gt;. I didn&#x27;t bother setting up swap, since I have 8GB of RAM in my laptop and that&#x27;s really enough for my use case.&lt;&#x2F;p&gt;
3200&lt;p&gt;With that done, we will now ask the configuration wizard to do some work for us (in particular, generate a template) with &lt;code&gt;nixos-generate-config --root &#x2F;mnt&lt;&#x2F;code&gt;. This generates a very well documented file that we should edit right now (and this is important!) with whatever editor you prefer. I used &lt;code&gt;vim&lt;&#x2F;code&gt;, but you can change it for &lt;code&gt;nano&lt;&#x2F;code&gt; if you prefer.&lt;&#x2F;p&gt;
3201&lt;p&gt;On to the configuration file, we need to enable a few things, so &lt;code&gt;vim &#x2F;mnt&#x2F;etc&#x2F;nixos&#x2F;configuration.nix&lt;&#x2F;code&gt; and start scrolling down. We want to make sure to uncomment:&lt;&#x2F;p&gt;
3202&lt;pre&gt;&lt;code&gt;# We really want network!
3203networking.wireless.enable = true;
3204
3205# This &amp;quot;fixes&amp;quot; the keyboard layout. Put the one you use.
3206i18n = {
3207consoleKeyMap = &amp;quot;es&amp;quot;;
3208}
3209
3210# Timezones are tricky so let&#x27;s get this right.
3211time.timeZone = &amp;quot;Europe&#x2F;Madrid&amp;quot;;
3212
3213# We *really* want some base packages installed, such as
3214# wpa_supplicant, or we won&#x27;t have a way to connect to the
3215# network once we install...
3216environment.systemPackages = with pkgs; [
3217wpa_supplicant wget curl vim neovim cmus mpv firefox git tdesktop
3218];
3219
3220# Printing is useful, sure, enable CUPS
3221services.printing.enable = true;
3222
3223# We have speakers, let&#x27;s make use of them.
3224sound.enable = true;
3225hardware.pulseaudio.enable = true;
3226
3227# We want the X11 windowing system enabled, in Spanish.
3228services.xserver.enable = true;
3229services.xserver.layout = &amp;quot;es&amp;quot;;
3230
3231# I want a desktop manager in my laptop.
3232# I personally prefer XFCE, but the manual shows plenty
3233# of other options, such as Plasma, i3 WM, or whatever.
3234services.xserver.desktopManager.xfce.enable = true;
3235services.xserver.desktopManager.default = &amp;quot;xfce&amp;quot;;
3236
3237# Touchpad is useful (although sometimes annoying) in a laptop
3238services.xserver.libinput.enable = true;
3239
3240# We don&#x27;t want to do everything as root!
3241users.users.lonami = {
3242isNormalUser = true;
3243uid = 1000;
3244home = &amp;quot;&#x2F;home&#x2F;lonami&amp;quot;;
3245extraGroups = [ &amp;quot;wheel&amp;quot; &amp;quot;networkmanager&amp;quot; &amp;quot;audio&amp;quot; ];
3246};
3247&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3248&lt;p&gt;&lt;em&gt;(Fun fact, I overlooked the configuration file until I wrote this and hadn&#x27;t noticed sound&#x2F;pulseaudio was there. It wasn&#x27;t hard to find online how to enable it though!)&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
3249&lt;p&gt;Now, let&#x27;s modify &lt;code&gt;hardware-configuration.nix&lt;&#x2F;code&gt;. But if you have &lt;code&gt;&#x2F;home&lt;&#x2F;code&gt; in a separate partition like me, you should run &lt;code&gt;blkid&lt;&#x2F;code&gt; to figure out its UUID. To avoid typing it out myself, I just ran &lt;code&gt;blkid &amp;gt;&amp;gt; &#x2F;mnt&#x2F;etc&#x2F;nixos&#x2F;hardware-configuration.nix&lt;&#x2F;code&gt; so that I could easily move it around with &lt;code&gt;vim&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
3250&lt;pre&gt;&lt;code&gt;# (stuff...)
3251
3252fileSystems.&amp;quot;&#x2F;home&amp;quot; =
3253{ device = &amp;quot;&#x2F;dev&#x2F;disk&#x2F;by-uuid&#x2F;d344c686-cae7-4dd3-840e-308eddf86608&amp;quot;;
3254fsType = &amp;quot;ext4&amp;quot;;
3255};
3256
3257# (more stuff...)
3258&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3259&lt;p&gt;Note that, obviously, you should put your own partition&#x27;s UUID there. Modifying the configuration is where I think the current NixOS&#x27; manual should have made more emphasis, at this step of the installation. They do detail it below, but that was already too late in my first attempt. Anyway, you can boot from the USB and run &lt;code&gt;nixos-install&lt;&#x2F;code&gt; as many times as you need until you get it working!&lt;&#x2F;p&gt;
3260&lt;p&gt;But before installing, we need to configure the network since there are plenty of things to download. If you want to work from WiFi, you should first figure out the name of your network card with &lt;code&gt;ip link show&lt;&#x2F;code&gt;. In my case it&#x27;s called &lt;code&gt;wlp3s0&lt;&#x2F;code&gt;. So with that knowledge we can run &lt;code&gt;wpa_supplicant -B -i wlp3s0 -c &amp;lt;(wpa_passphrase SSID key)&lt;&#x2F;code&gt;. Be sure to replace both &lt;code&gt;SSID&lt;&#x2F;code&gt; and &lt;code&gt;key&lt;&#x2F;code&gt; with the name of your network and password key, respectively. If they have spaces, surround them in quotes.&lt;&#x2F;p&gt;
3261&lt;p&gt;Another funny pitfall was typing &lt;code&gt;wpa_supplicant&lt;&#x2F;code&gt; in the command above twice (instead of &lt;code&gt;wpa_passphrase&lt;&#x2F;code&gt;). That sure spit out a few funny errors! Once you have ran that, wait a few seconds and &lt;code&gt;ping 1.1.1.1&lt;&#x2F;code&gt; to make sure that you can reach the internet. If you do, &lt;code&gt;^C&lt;&#x2F;code&gt; and let&#x27;s install NixOS!&lt;&#x2F;p&gt;
3262&lt;pre&gt;&lt;code&gt;nixos-install
3263&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3264&lt;p&gt;Well, that was pretty painless. You can now &lt;code&gt;reboot&lt;&#x2F;code&gt; and enjoy your new, functional system.&lt;&#x2F;p&gt;
3265&lt;h2 id=&quot;afterword&quot;&gt;Afterword&lt;&#x2F;h2&gt;
3266&lt;p&gt;The process of installing NixOS was really painless once you have made sense out of what things mean. I was far more pleased this time than in my previous attempt, despite the four attempts I needed to have it up and running.&lt;&#x2F;p&gt;
3267&lt;p&gt;However not all is so good. I&#x27;m not sure where I went wrong, but the first time I tried with &lt;code&gt;i3&lt;&#x2F;code&gt; instead of &lt;code&gt;xfce&lt;&#x2F;code&gt;, all I was welcome with was a white, small terminal in the top left corner. I even generated a configuration file with &lt;code&gt;i3-config-wizard&lt;&#x2F;code&gt; to make sure it could detect my Mod1&#x2F;Mod4 keys (which, it did), but even after rebooting, my commands weren&#x27;t responding. For example, I couldn&#x27;t manage to open another terminal with &lt;code&gt;Mod1+Enter&lt;&#x2F;code&gt;. I&#x27;m not even sure that I was in &lt;code&gt;i3&lt;&#x2F;code&gt;…&lt;&#x2F;p&gt;
3268&lt;p&gt;In my very first attempt, I pressed &lt;code&gt;Alt+F8&lt;&#x2F;code&gt; as suggested in the welcome message. This took me an offline copy of the manual, which is really nicely done. Funny enough, though, I couldn&#x27;t exit &lt;code&gt;w3m&lt;&#x2F;code&gt;. Both &lt;code&gt;Q&lt;&#x2F;code&gt; and &lt;code&gt;B&lt;&#x2F;code&gt; to quit and take me back wouldn&#x27;t work. Somehow, it kept throwing me back into &lt;code&gt;w3m&lt;&#x2F;code&gt;, so I had to forcibly shutdown.&lt;&#x2F;p&gt;
3269&lt;p&gt;In my second attempt, I also forgot to configure network, so I had no way to download &lt;code&gt;wpa_supplicant&lt;&#x2F;code&gt; without having &lt;code&gt;wpa_supplicant&lt;&#x2F;code&gt; itself to connect my laptop to the network! So, it was important to do that through the USB before installing it (which comes with the program preinstalled), just by making sure to add it in the configuration file.&lt;&#x2F;p&gt;
3270&lt;p&gt;Some other notes, if you can&#x27;t reach the internet, don&#x27;t add any DNS in &lt;code&gt;&#x2F;etc&#x2F;resolv.conf&lt;&#x2F;code&gt;. This should be done declaratively in &lt;code&gt;configuration.nix&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3271&lt;p&gt;In the end, I spent the entire afternoon playing around with it, taking breaks and what-not. I still haven&#x27;t figured out why &lt;code&gt;nvim&lt;&#x2F;code&gt; was printing the literal escape character when going from normal to insert mode in the &lt;code&gt;xfce4-terminal&lt;&#x2F;code&gt; (and other actions also made it print this &amp;quot;garbage&amp;quot; to the console), why sometimes the network can reach the internet (and only some sites!) and sometimes not, and how to setup dualboot.&lt;&#x2F;p&gt;
3272&lt;p&gt;But despite all of this, I think it was a worth installing it again. One sure sees things from a different perspective, and gets the chance to write another blog post!&lt;&#x2F;p&gt;
3273&lt;p&gt;If there&#x27;s something I overlooked or that could be done better, or maybe you can explain it differently, please be sure to &lt;a href=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;contact&quot;&gt;contact me&lt;&#x2F;a&gt; to let me know!&lt;&#x2F;p&gt;
3274&lt;h2 id=&quot;update&quot;&gt;Update&lt;&#x2F;h2&gt;
3275&lt;p&gt;Well, that was surprisingly fast feedback. Thank you very much &lt;a href=&quot;https:&#x2F;&#x2F;bb010g.keybase.pub&#x2F;&quot;&gt;@bb010g&lt;&#x2F;a&gt; for it! As they rightfully pointed out, one can avoid adding &lt;code&gt;&#x2F;home&lt;&#x2F;code&gt; manually to &lt;code&gt;hardware-configuration.nix&lt;&#x2F;code&gt; if you mount it before generating the configuration files. However, the installation process doesn&#x27;t need &lt;code&gt;&#x2F;home&lt;&#x2F;code&gt; mounted, so I didn&#x27;t do it.&lt;&#x2F;p&gt;
3276&lt;p&gt;The second weird issue with &lt;code&gt;w3m&lt;&#x2F;code&gt; is actually a funny one. &lt;code&gt;Alt+F8&lt;&#x2F;code&gt; &lt;em&gt;switches to another TTY&lt;&#x2F;em&gt;! That&#x27;s why quitting the program wouldn&#x27;t do anything. You&#x27;d still be in a different TTY! Normally, this is &lt;code&gt;Ctrl+Alt+FX&lt;&#x2F;code&gt;, so I hadn&#x27;t even thought that this is what could be happening. Anyway, the solution is not quitting the program, but rather going back to the main TTY with &lt;code&gt;Alt+F1&lt;&#x2F;code&gt;. You can switch back and forth all you need to consult the manual.&lt;&#x2F;p&gt;
3277&lt;p&gt;More suggestions are having &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;rycee&#x2F;home-manager&quot;&gt;&lt;code&gt;home-manager&lt;&#x2F;code&gt;&lt;&#x2F;a&gt; manage the graphical sessions, since it should be easier to deal with than the alternatives.&lt;&#x2F;p&gt;
3278&lt;p&gt;Despite having followed the guide and having read it over and over several times, it seems like my thoughts in this blog post may be a bit messy. So I recommend you also reading through the guide to have two versions of all this, just in case.&lt;&#x2F;p&gt;
3279&lt;p&gt;Regarding network issues, they use &lt;code&gt;connman&lt;&#x2F;code&gt; so that may be worth checking out.&lt;&#x2F;p&gt;
3280&lt;p&gt;Regarding terminal issues with &lt;code&gt;nvim&lt;&#x2F;code&gt; printing the literal escape character, I was told off for not having checked what my &lt;code&gt;$TERM&lt;&#x2F;code&gt; was. I hadn&#x27;t really looked into it much myself, just complained about it here, so sorry for being annoying about that. A quick search in the &lt;code&gt;nixpkgs&lt;&#x2F;code&gt; repository lets us find &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;NixOS&#x2F;nixpkgs&#x2F;blob&#x2F;release-18.09&#x2F;pkgs&#x2F;applications&#x2F;editors&#x2F;neovim&#x2F;default.nix&quot;&gt;neovim&#x2F;default.nix&lt;&#x2F;a&gt;, with version 0.3.1. Looking at &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;neovim&#x2F;neovim&quot;&gt;Neovim&#x27;s main repository&lt;&#x2F;a&gt; we can see that this is a bit outdated, but that is fine.&lt;&#x2F;p&gt;
3281&lt;p&gt;If only I had bothered to look at &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;neovim&#x2F;neovim&#x2F;wiki&#x2F;FAQ#nvim-shows-weird-symbols-2-q-when-changing-modes&quot;&gt;Neovim&#x27;s wiki&lt;&#x2F;a&gt;, (which they found through &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;neovim&#x2F;neovim&#x2F;issues&#x2F;7749&quot;&gt;Neovim&#x27;s GitHub issues&lt;&#x2F;a&gt;) I would&#x27;ve seen that some terminals just don&#x27;t support the program properly. The solution is, of course, to use a different terminal emulator with better support or to disable the &lt;code&gt;guicursor&lt;&#x2F;code&gt; in Neovim&#x27;s config.&lt;&#x2F;p&gt;
3282&lt;p&gt;This is a pretty good life lesson. 30 seconds of searching, maybe two minutes and a half for also checking XFCE issues, are often more than enough to troubleshoot your issues. The internet is a big place and more people have surely came across the problem before, so make sure to look online first. In my defense I&#x27;ll say that it didn&#x27;t bother me so much so I didn&#x27;t bother looking for that soon either.&lt;&#x2F;p&gt;
3283</content>
3284	</entry>
3285	<entry xml:lang="en">
3286		<title>Breaking Risk of Rain</title>
3287		<published>2019-01-12T00:00:00+00:00</published>
3288		<updated>2019-01-12T00:00:00+00:00</updated>
3289		<link href="https://lonami.dev/blog/breaking-ror/" type="text/html"/>
3290		<id>https://lonami.dev/blog/breaking-ror/</id>
3291		<content type="html">&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;riskofraingame.com&#x2F;&quot;&gt;Risk of Rain&lt;&#x2F;a&gt; is a fun little game you can spend a lot of hours on. It&#x27;s incredibly challenging for new players, and fun once you have learnt the basics. This blog will go through what I&#x27;ve learnt and how to play the game correctly.&lt;&#x2F;p&gt;
3292&lt;h2 id=&quot;getting-started&quot;&gt;Getting Started&lt;&#x2F;h2&gt;
3293&lt;p&gt;If you&#x27;re new to the game, you may find it frustrating. You must learn very well to dodge.&lt;&#x2F;p&gt;
3294&lt;p&gt;Your first &lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Category:Characters&quot;&gt;character&lt;&#x2F;a&gt; will be &lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Commando&quot;&gt;Commando&lt;&#x2F;a&gt;. He&#x27;s actually a very nice character. Use your third skill (dodge) to move faster, pass through large groups of enemies, and negate fall damage.&lt;&#x2F;p&gt;
3295&lt;p&gt;If there are a lot of monsters, remember to &lt;strong&gt;leave&lt;&#x2F;strong&gt; from there! It&#x27;s really important for survival. Most enemies &lt;strong&gt;don&#x27;t do body damage&lt;&#x2F;strong&gt;. Not even the body of the &lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Magma_Worm&quot;&gt;Magma Worm&lt;&#x2F;a&gt; or the &lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Wandering_Vagrant&quot;&gt;Wandering Vagrant&lt;&#x2F;a&gt; (just dodge the head and projectiles respectively).&lt;&#x2F;p&gt;
3296&lt;p&gt;The first thing you must do is always &lt;strong&gt;rush for the teleporter&lt;&#x2F;strong&gt;. Completing the levels quick will make the game easier. But make sure to take note of &lt;strong&gt;where the chests are&lt;&#x2F;strong&gt;! When you have time (even when the countdown finishes), go back for them and buy as many as you can. Generally, prefer &lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Chest&quot;&gt;chests&lt;&#x2F;a&gt; over &lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Shrine&quot;&gt;shrines&lt;&#x2F;a&gt; since they may eat all your money.&lt;&#x2F;p&gt;
3297&lt;p&gt;Completing the game on &lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Difficulty&quot;&gt;Drizzle&lt;&#x2F;a&gt; is really easy if you follow these tips.&lt;&#x2F;p&gt;
3298&lt;h2 id=&quot;requisites&quot;&gt;Requisites&lt;&#x2F;h2&gt;
3299&lt;p&gt;Before breaking the game, you must obtain several &lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Item#Artifacts&quot;&gt;artifacts&lt;&#x2F;a&gt;. We are interested in particular in the following:&lt;&#x2F;p&gt;
3300&lt;ul&gt;
3301&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Sacrifice&quot;&gt;Sacrifice&lt;&#x2F;a&gt;. You really need this one, and may be a bit hard to get. With it, you will be able to farm the first level for 30 minutes and kill the final boss in 30 seconds.&lt;&#x2F;li&gt;
3302&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Command&quot;&gt;Command&lt;&#x2F;a&gt;. You need this unless you want to grind for hours to get enough of the items you really need for the rest of the game. Getting this one is easy.&lt;&#x2F;li&gt;
3303&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Glass&quot;&gt;Glass&lt;&#x2F;a&gt;. Your life will be very small (at the beginning…), but you will be able to one-shot everything easily.&lt;&#x2F;li&gt;
3304&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Kin&quot;&gt;Kin&lt;&#x2F;a&gt; (optional). It makes it easier to obtain a lot of boxes if you restart the first level until you get &lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Lemurian&quot;&gt;lemurians&lt;&#x2F;a&gt; or &lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Jellyfish&quot;&gt;jellyfish&lt;&#x2F;a&gt; as the monster, since they&#x27;re cheap to spawn.&lt;&#x2F;li&gt;
3305&lt;&#x2F;ul&gt;
3306&lt;p&gt;With those, the game becomes trivial. Playing as &lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Huntress&quot;&gt;Huntress&lt;&#x2F;a&gt; is excellent since she can move at high speed while killing everything on screen.&lt;&#x2F;p&gt;
3307&lt;h2 id=&quot;breaking-the-game&quot;&gt;Breaking the Game&lt;&#x2F;h2&gt;
3308&lt;p&gt;The rest is easy! With the command artifact you want the following items.&lt;&#x2F;p&gt;
3309&lt;h3 id=&quot;common-items&quot;&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Category:Common_Items&quot;&gt;Common Items&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
3310&lt;ul&gt;
3311&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Soldier&amp;#x27;s_Syringe&quot;&gt;Soldier&#x27;s Syringe&lt;&#x2F;a&gt;. &lt;strong&gt;Stack 13&lt;&#x2F;strong&gt; of these and you will triple your attack speed. You can get started with 4 or so.&lt;&#x2F;li&gt;
3312&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Paul&amp;#x27;s_Goat_Hoof&quot;&gt;Paul&#x27;s Goat Hoof&lt;&#x2F;a&gt;. &lt;strong&gt;Stack +30&lt;&#x2F;strong&gt; of these and your movement speed will be insane. You can get a very good speed with 8 or so.&lt;&#x2F;li&gt;
3313&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Crowbar&quot;&gt;Crowbar&lt;&#x2F;a&gt;. &lt;strong&gt;Stack +20&lt;&#x2F;strong&gt; to guarantee you can one-shot bosses.&lt;&#x2F;li&gt;
3314&lt;&#x2F;ul&gt;
3315&lt;p&gt;If you want to be safer:&lt;&#x2F;p&gt;
3316&lt;ul&gt;
3317&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Hermit&amp;#x27;s_Scarf&quot;&gt;Hermit&#x27;s Scarf&lt;&#x2F;a&gt;. &lt;strong&gt;Stack 6&lt;&#x2F;strong&gt; of these to dodge 1&#x2F;3 of the attacks.&lt;&#x2F;li&gt;
3318&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Monster_Tooth&quot;&gt;Monster Tooth&lt;&#x2F;a&gt;. &lt;strong&gt;Stack 9&lt;&#x2F;strong&gt; of these to recover 50 life on kill. This is plenty, since you will be killing &lt;em&gt;a lot&lt;&#x2F;em&gt;.&lt;&#x2F;li&gt;
3319&lt;&#x2F;ul&gt;
3320&lt;p&gt;If you don&#x27;t have enough and want more fun, get one of these:&lt;&#x2F;p&gt;
3321&lt;ul&gt;
3322&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Gasoline&quot;&gt;Gasoline&lt;&#x2F;a&gt;. Burn the ground on kill, and more will die!&lt;&#x2F;li&gt;
3323&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Headstompers&quot;&gt;Headstompers&lt;&#x2F;a&gt;. They make a pleasing sound on fall, and hurt.&lt;&#x2F;li&gt;
3324&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Lens-Maker&amp;#x27;s_Glasses&quot;&gt;Lens-Maker&#x27;s Glasses&lt;&#x2F;a&gt;. &lt;strong&gt;Stack 14&lt;&#x2F;strong&gt; and you will always deal a critical strike for double the damage.&lt;&#x2F;li&gt;
3325&lt;&#x2F;ul&gt;
3326&lt;h3 id=&quot;uncommon-items&quot;&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Category:Uncommon_Items&quot;&gt;Uncommon Items&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
3327&lt;ul&gt;
3328&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Infusion&quot;&gt;Infusion&lt;&#x2F;a&gt;. You only really need one of this. Your life will skyrocket after a while, since this gives you 1HP per kill.&lt;&#x2F;li&gt;
3329&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Hopoo_Feather&quot;&gt;Hopoo Feather&lt;&#x2F;a&gt;. &lt;strong&gt;Stack +10&lt;&#x2F;strong&gt; of these. You will pretty much be able to fly with so many jumps.&lt;&#x2F;li&gt;
3330&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Guardian&amp;#x27;s_Heart&quot;&gt;Guardian&#x27;s Heart&lt;&#x2F;a&gt;. Not really necessary, but useful for early and late game, since it will absorb infinite damage the first hit.&lt;&#x2F;li&gt;
3331&lt;&#x2F;ul&gt;
3332&lt;p&gt;If, again, you want more fun, get one of these:&lt;&#x2F;p&gt;
3333&lt;ul&gt;
3334&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Ukulele&quot;&gt;Ukelele&lt;&#x2F;a&gt;. Spazz your enemies!&lt;&#x2F;li&gt;
3335&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Will-o&amp;#x27;-the-wisp&quot;&gt;Will-o&#x27;-the-wisp&lt;&#x2F;a&gt;. Explode your enemies!&lt;&#x2F;li&gt;
3336&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Chargefield_Generator&quot;&gt;Chargefield Generator&lt;&#x2F;a&gt;. It should cover your entire screen after a bit, hurting all enemies without moving a finger.&lt;&#x2F;li&gt;
3337&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Golden_Gun&quot;&gt;Golden Gun&lt;&#x2F;a&gt;. You will be rich, so this gives you +40% damage.&lt;&#x2F;li&gt;
3338&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Predatory_Instincts&quot;&gt;Predatory Instincts&lt;&#x2F;a&gt;. If you got 14 glasses, you will always be doing critical strikes, and this will give even more attack speed.&lt;&#x2F;li&gt;
3339&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;56_Leaf_Clover&quot;&gt;56 Leaf Clover&lt;&#x2F;a&gt;. More drops, in case you didn&#x27;t have enough.&lt;&#x2F;li&gt;
3340&lt;&#x2F;ul&gt;
3341&lt;h3 id=&quot;rare-items&quot;&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Category:Rare_Items&quot;&gt;Rare Items&lt;&#x2F;a&gt;&lt;&#x2F;h3&gt;
3342&lt;ul&gt;
3343&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Ceremonial_Dagger&quot;&gt;Ceremonial Dagger&lt;&#x2F;a&gt;. &lt;strong&gt;Stack +3&lt;&#x2F;strong&gt;, then killing one thing kills another thing and makes a chain reaction.&lt;&#x2F;li&gt;
3344&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Alien_Head&quot;&gt;Alien Head&lt;&#x2F;a&gt;. &lt;strong&gt;Stack 3&lt;&#x2F;strong&gt;, and you will be able to use your abilities more often.&lt;&#x2F;li&gt;
3345&lt;&#x2F;ul&gt;
3346&lt;p&gt;For more fun:&lt;&#x2F;p&gt;
3347&lt;ul&gt;
3348&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;riskofrain.wikia.com&#x2F;wiki&#x2F;Brilliant_Behemoth&quot;&gt;Brilliant Behemoth&lt;&#x2F;a&gt;. Boom boom.&lt;&#x2F;li&gt;
3349&lt;&#x2F;ul&gt;
3350&lt;h2 id=&quot;closing-words&quot;&gt;Closing Words&lt;&#x2F;h2&gt;
3351&lt;p&gt;You can now beat the game in Monsoon solo with any character. Have fun! And be careful with the sadly common crashes.&lt;&#x2F;p&gt;
3352</content>
3353	</entry>
3354	<entry xml:lang="en">
3355		<title>WorldEdit Commands</title>
3356		<published>2018-07-11T00:00:00+00:00</published>
3357		<updated>2018-07-11T00:00:00+00:00</updated>
3358		<link href="https://lonami.dev/blog/world-edit/" type="text/html"/>
3359		<id>https://lonami.dev/blog/world-edit/</id>
3360		<content type="html">&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;dev.bukkit.org&#x2F;projects&#x2F;worldedit&quot;&gt;WorldEdit&lt;&#x2F;a&gt; is an extremely powerful tool for modifying entire worlds within &lt;a href=&quot;https:&#x2F;&#x2F;minecraft.net&quot;&gt;Minecraft&lt;&#x2F;a&gt;, which can be used as either a mod for your single-player worlds or as a plugin for your &lt;a href=&quot;https:&#x2F;&#x2F;getbukkit.org&#x2F;&quot;&gt;Bukkit&lt;&#x2F;a&gt; servers.&lt;&#x2F;p&gt;
3361&lt;p&gt;This command guide was written for Minecraft 1.12.1, version &lt;a href=&quot;https:&#x2F;&#x2F;dev.bukkit.org&#x2F;projects&#x2F;worldedit&#x2F;files&#x2F;2460562&quot;&gt;6.1.7.3&lt;&#x2F;a&gt;, but should work for newer versions too. All WorldEdit commands can be used with a double slash (&lt;code&gt;&#x2F;&#x2F;&lt;&#x2F;code&gt;) so they don&#x27;t conlict with built-in commands. This means you can get a list of all commands with &lt;code&gt;&#x2F;&#x2F;help&lt;&#x2F;code&gt;. Let&#x27;s explore different categories!&lt;&#x2F;p&gt;
3362&lt;h2 id=&quot;movement&quot;&gt;Movement&lt;&#x2F;h2&gt;
3363&lt;p&gt;In order to edit a world properly you need to learn how to move in said world properly. There are several straightforward commands that let you move:&lt;&#x2F;p&gt;
3364&lt;ul&gt;
3365&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;ascend&lt;&#x2F;code&gt; goes up one floor.&lt;&#x2F;li&gt;
3366&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;descend&lt;&#x2F;code&gt; goes down one floor.&lt;&#x2F;li&gt;
3367&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;thru&lt;&#x2F;code&gt; let&#x27;s you pass through walls.&lt;&#x2F;li&gt;
3368&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;jumpto&lt;&#x2F;code&gt; to go wherever you are looking.&lt;&#x2F;li&gt;
3369&lt;&#x2F;ul&gt;
3370&lt;h2 id=&quot;information&quot;&gt;Information&lt;&#x2F;h2&gt;
3371&lt;p&gt;Knowing your world properly is as important as knowing how to move within it, and will also let you change the information in said world if you need to.&lt;&#x2F;p&gt;
3372&lt;ul&gt;
3373&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;biomelist&lt;&#x2F;code&gt; shows all known biomes.&lt;&#x2F;li&gt;
3374&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;biomeinfo&lt;&#x2F;code&gt; shows the current biome.&lt;&#x2F;li&gt;
3375&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;setbiome&lt;&#x2F;code&gt; lets you change the biome.&lt;&#x2F;li&gt;
3376&lt;&#x2F;ul&gt;
3377&lt;h2 id=&quot;blocks&quot;&gt;Blocks&lt;&#x2F;h2&gt;
3378&lt;p&gt;You can act over all blocks in a radius around you with quite a few commands. Some won&#x27;t actually act over the entire range you specify, so 100 is often a good number.&lt;&#x2F;p&gt;
3379&lt;h3 id=&quot;filling&quot;&gt;Filling&lt;&#x2F;h3&gt;
3380&lt;p&gt;You can fill pools with &lt;code&gt;&#x2F;&#x2F;fill water 100&lt;&#x2F;code&gt; or caves with &lt;code&gt;&#x2F;&#x2F;fillr water 100&lt;&#x2F;code&gt;, both of which act below your feet.&lt;&#x2F;p&gt;
3381&lt;h3 id=&quot;fixing&quot;&gt;Fixing&lt;&#x2F;h3&gt;
3382&lt;p&gt;If the water or lava is buggy use &lt;code&gt;&#x2F;&#x2F;fixwater 100&lt;&#x2F;code&gt; or &lt;code&gt;&#x2F;&#x2F;fixlava 100&lt;&#x2F;code&gt; respectively.&lt;&#x2F;p&gt;
3383&lt;p&gt;Some creeper removed the snow or the grass? Fear not, you can use &lt;code&gt;&#x2F;&#x2F;snow 10&lt;&#x2F;code&gt; or &lt;code&gt;&#x2F;&#x2F;grass 10&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3384&lt;h3 id=&quot;emptying&quot;&gt;Emptying&lt;&#x2F;h3&gt;
3385&lt;p&gt;You can empty a pool completely with &lt;code&gt;&#x2F;&#x2F;drain 100&lt;&#x2F;code&gt;, remove the snow with &lt;code&gt;&#x2F;&#x2F;thaw 10&lt;&#x2F;code&gt;, and remove fire with &lt;code&gt;&#x2F;&#x2F;ex 10&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3386&lt;h3 id=&quot;removing&quot;&gt;Removing&lt;&#x2F;h3&gt;
3387&lt;p&gt;You can remove blocks above and below you in some area with the &lt;code&gt;&#x2F;&#x2F;removeabove N&lt;&#x2F;code&gt; and &lt;code&gt;&#x2F;&#x2F;removebelow N&lt;&#x2F;code&gt;. You probably want to set a limit though, or you could fall off the world with &lt;code&gt;&#x2F;&#x2F;removebelow 1 10&lt;&#x2F;code&gt; for radius and depth. You can also remove near blocks with &lt;code&gt;&#x2F;&#x2F;removenear block 10&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3388&lt;h3 id=&quot;shapes&quot;&gt;Shapes&lt;&#x2F;h3&gt;
3389&lt;p&gt;Making a cylinder (or circle) can be done with through &lt;code&gt;&#x2F;&#x2F;cyl stone 10&lt;&#x2F;code&gt;, a third argument for the height. The radius can be comma-separated to make a ellipses instead, such as &lt;code&gt;&#x2F;&#x2F;cyl stone 5,10&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3390&lt;p&gt;Spheres are done with &lt;code&gt;&#x2F;&#x2F;sphere stone 5&lt;&#x2F;code&gt;. This will build one right at your center, so you can raise it to be on your feet with &lt;code&gt;&#x2F;&#x2F;sphere stone 5 yes&lt;&#x2F;code&gt;. Similar to cylinders, you can comma separate the radius &lt;code&gt;x,y,z&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3391&lt;p&gt;Pyramids can be done with &lt;code&gt;&#x2F;&#x2F;pyramic stone 5&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3392&lt;p&gt;All these commands can be prefixed with &amp;quot;h&amp;quot; to make them hollow. For instance, &lt;code&gt;&#x2F;&#x2F;hsphere stone 10&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3393&lt;h2 id=&quot;regions&quot;&gt;Regions&lt;&#x2F;h2&gt;
3394&lt;h3 id=&quot;basics&quot;&gt;Basics&lt;&#x2F;h3&gt;
3395&lt;p&gt;Operating over an entire region is really important, and the first thing you need to work comfortably with them is a tool to make selections. The default wooden-axe tool can be obtained with &lt;code&gt;&#x2F;&#x2F;wand&lt;&#x2F;code&gt;, but you must be near the blocks to select. You can use a different tool, like a golden axe, to use as your &amp;quot;far wand&amp;quot; (wand usable over distance). Once you have one in your hand type &lt;code&gt;&#x2F;&#x2F;farwand&lt;&#x2F;code&gt; to use it as your &amp;quot;far wand&amp;quot;. You can select the two corners of your region with left and right click. If you have selected the wrong tool, use &lt;code&gt;&#x2F;&#x2F;none&lt;&#x2F;code&gt; to clear it.&lt;&#x2F;p&gt;
3396&lt;p&gt;If there are no blocks but you want to use your current position as a corner, use &lt;code&gt;&#x2F;&#x2F;pos1&lt;&#x2F;code&gt; or 2.&lt;&#x2F;p&gt;
3397&lt;p&gt;If you made a region too small, you can enlarge it with &lt;code&gt;&#x2F;&#x2F;expand 10 up&lt;&#x2F;code&gt;, or &lt;code&gt;&#x2F;&#x2F;expand vert&lt;&#x2F;code&gt; for the entire vertical range, etc., or make it smaller with &lt;code&gt;&#x2F;&#x2F;contract 10 up&lt;&#x2F;code&gt; etc., or &lt;code&gt;&#x2F;&#x2F;inset&lt;&#x2F;code&gt; it to contract in both directions. You can use short-names for the cardinal directions (NSEW).&lt;&#x2F;p&gt;
3398&lt;p&gt;Finally, if you want to move your selection, you can &lt;code&gt;&#x2F;&#x2F;shift 1 north&lt;&#x2F;code&gt; it to wherever you need.&lt;&#x2F;p&gt;
3399&lt;h3 id=&quot;information-1&quot;&gt;Information&lt;&#x2F;h3&gt;
3400&lt;p&gt;You can get the &lt;code&gt;&#x2F;&#x2F;size&lt;&#x2F;code&gt; of the selection or even &lt;code&gt;&#x2F;&#x2F;count torch&lt;&#x2F;code&gt; in some area. If you want to count all blocks, get their distribution &lt;code&gt;&#x2F;&#x2F;distr&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3401&lt;h3 id=&quot;filling-1&quot;&gt;Filling&lt;&#x2F;h3&gt;
3402&lt;p&gt;With a region selected, you can &lt;code&gt;&#x2F;&#x2F;set&lt;&#x2F;code&gt; it to be any block! For instance, you can use &lt;code&gt;&#x2F;&#x2F;set air&lt;&#x2F;code&gt; to clear it entirely. You can use more than one block evenly by separting them with a comma &lt;code&gt;&#x2F;&#x2F;set stone,dirt&lt;&#x2F;code&gt;, or with a custom chance &lt;code&gt;&#x2F;&#x2F;set 20%stone,80%dirt&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3403&lt;p&gt;You can use &lt;code&gt;&#x2F;&#x2F;replace from to&lt;&#x2F;code&gt; instead if you don&#x27;t want to override all blocks in your selection.&lt;&#x2F;p&gt;
3404&lt;p&gt;You can make an hollow set with &lt;code&gt;&#x2F;&#x2F;faces&lt;&#x2F;code&gt;, and if you just want the walls, use &lt;code&gt;&#x2F;&#x2F;walls&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3405&lt;h3 id=&quot;cleaning&quot;&gt;Cleaning&lt;&#x2F;h3&gt;
3406&lt;p&gt;If someone destroyed your wonderful snow landscape, fear not, you can use &lt;code&gt;&#x2F;&#x2F;overlay snow&lt;&#x2F;code&gt; over it (although for this you actually have &lt;code&gt;&#x2F;&#x2F;snow N&lt;&#x2F;code&gt; and its opposite &lt;code&gt;&#x2F;&#x2F;thaw&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
3407&lt;p&gt;If you set some rough area, you can always &lt;code&gt;&#x2F;&#x2F;smooth&lt;&#x2F;code&gt; it, even more than one time with &lt;code&gt;&#x2F;&#x2F;smooth 3&lt;&#x2F;code&gt;. You can get your dirt and stone back with &lt;code&gt;&#x2F;&#x2F;naturalize&lt;&#x2F;code&gt; and put some plants with &lt;code&gt;&#x2F;&#x2F;flora&lt;&#x2F;code&gt; or &lt;code&gt;&#x2F;&#x2F;forest&lt;&#x2F;code&gt;, both of which support a density or even the type for the trees. If you already have the dirt use &lt;code&gt;&#x2F;&#x2F;green&lt;&#x2F;code&gt; instead. If you want some pumpkins, with &lt;code&gt;&#x2F;&#x2F;pumpkins&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3408&lt;h3 id=&quot;moving&quot;&gt;Moving&lt;&#x2F;h3&gt;
3409&lt;p&gt;You can repeat an entire selection many times by stacking them with &lt;code&gt;&#x2F;&#x2F;stack N DIR&lt;&#x2F;code&gt;. This is extremely useful to make things like corridors or elevators. For instance, you can make a small section of the corridor, select it entirely, and then repeat it 10 times with &lt;code&gt;&#x2F;&#x2F;stack 10 north&lt;&#x2F;code&gt;. Or you can make the elevator and then &lt;code&gt;&#x2F;&#x2F;stack 10 up&lt;&#x2F;code&gt;. If you need to also copy the air use &lt;code&gt;&#x2F;&#x2F;stackair&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3410&lt;p&gt;Finally, if you don&#x27;t need to repeat it and simply move it just a bit towards the right direction, you can use &lt;code&gt;&#x2F;&#x2F;move N&lt;&#x2F;code&gt;. The default direction is &amp;quot;me&amp;quot; (towards where you are facing) but you can set one with &lt;code&gt;&#x2F;&#x2F;move 1 up&lt;&#x2F;code&gt; for example.&lt;&#x2F;p&gt;
3411&lt;h3 id=&quot;selecting&quot;&gt;Selecting&lt;&#x2F;h3&gt;
3412&lt;p&gt;You can not only select cuboids. You can also select different shapes, or even just points:&lt;&#x2F;p&gt;
3413&lt;ul&gt;
3414&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;sel cuboid&lt;&#x2F;code&gt; is the default.&lt;&#x2F;li&gt;
3415&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;sel extend&lt;&#x2F;code&gt; expands the default.&lt;&#x2F;li&gt;
3416&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;sel poly&lt;&#x2F;code&gt; first point with left click and right click to add new points.&lt;&#x2F;li&gt;
3417&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;sel ellipsoid&lt;&#x2F;code&gt; first point to select the center and right click to select the different radius.&lt;&#x2F;li&gt;
3418&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;sel sphere&lt;&#x2F;code&gt; first point to select the center and one more right click for the radius.&lt;&#x2F;li&gt;
3419&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;sel cyl&lt;&#x2F;code&gt; for cylinders, first click being the center.&lt;&#x2F;li&gt;
3420&lt;li&gt;&lt;code&gt;&#x2F;&#x2F;sel convex&lt;&#x2F;code&gt; for convex shapes. This one is extremely useful for &lt;code&gt;&#x2F;&#x2F;curve&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
3421&lt;&#x2F;ul&gt;
3422&lt;h2 id=&quot;brushes&quot;&gt;Brushes&lt;&#x2F;h2&gt;
3423&lt;p&gt;Brushes are a way to paint in 3D without first bothering about making a selection, and there are spherical and cylinder brushes with e.g. &lt;code&gt;&#x2F;&#x2F;brush sphere stone 2&lt;&#x2F;code&gt;, or the shorter form &lt;code&gt;&#x2F;&#x2F;br s stone&lt;&#x2F;code&gt;. For cylinder, one must use &lt;code&gt;cyl&lt;&#x2F;code&gt; instead &lt;code&gt;sphere&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3424&lt;p&gt;There also exists a brush to smooth the terrain which can be enabled on the current item with &lt;code&gt;&#x2F;&#x2F;br smooth&lt;&#x2F;code&gt;, which can be used with right-click like any other brush.&lt;&#x2F;p&gt;
3425&lt;h2 id=&quot;clipboard&quot;&gt;Clipboard&lt;&#x2F;h2&gt;
3426&lt;p&gt;Finally, you can copy and cut things around like you would do with normal text with &lt;code&gt;&#x2F;&#x2F;copy&lt;&#x2F;code&gt; and &lt;code&gt;&#x2F;&#x2F;cut&lt;&#x2F;code&gt;. The copy is issued from wherever you issue the command, so when you use &lt;code&gt;&#x2F;&#x2F;paste&lt;&#x2F;code&gt;, remember that if you were 4 blocks apart when copying, it will be 4 blocks apart when pasting.&lt;&#x2F;p&gt;
3427&lt;p&gt;The contents of the clipboard can be flipped to wherever you are looking via &lt;code&gt;&#x2F;&#x2F;flip&lt;&#x2F;code&gt;, and can be rotated via the &lt;code&gt;&#x2F;&#x2F;rotate 90&lt;&#x2F;code&gt; command (in degrees).&lt;&#x2F;p&gt;
3428&lt;p&gt;To remove the copy use &lt;code&gt;&#x2F;&#x2F;clearclipboard&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3429</content>
3430	</entry>
3431	<entry xml:lang="en">
3432		<title>An Introduction to Asyncio</title>
3433		<published>2018-06-13T00:00:00+00:00</published>
3434		<updated>2020-10-03T00:00:00+00:00</updated>
3435		<link href="https://lonami.dev/blog/asyncio/" type="text/html"/>
3436		<id>https://lonami.dev/blog/asyncio/</id>
3437		<content type="html">&lt;h2 id=&quot;index&quot;&gt;Index&lt;&#x2F;h2&gt;
3438&lt;ul&gt;
3439&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;asyncio&#x2F;#background&quot;&gt;Background&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
3440&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;asyncio&#x2F;#input_output&quot;&gt;Input &#x2F; Output&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
3441&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;asyncio&#x2F;#diving_in&quot;&gt;Diving In&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
3442&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;asyncio&#x2F;#a_toy_example&quot;&gt;A Toy Example&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
3443&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;asyncio&#x2F;#a_real_example&quot;&gt;A Real Example&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
3444&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;asyncio&#x2F;#extra_material&quot;&gt;Extra Material&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
3445&lt;&#x2F;ul&gt;
3446&lt;h2 id=&quot;background&quot;&gt;Background&lt;&#x2F;h2&gt;
3447&lt;p&gt;After seeing some friends struggle with &lt;code&gt;asyncio&lt;&#x2F;code&gt; I decided that it could be a good idea to write a blog post using my own words to explain how I understand the world of asynchronous IO. I will focus on Python&#x27;s &lt;code&gt;asyncio&lt;&#x2F;code&gt; module but this post should apply to any other language easily.&lt;&#x2F;p&gt;
3448&lt;p&gt;So what is &lt;code&gt;asyncio&lt;&#x2F;code&gt; and what makes it good? Why don&#x27;t we just use the old and known threads to run several parts of the code concurrently, at the same time?&lt;&#x2F;p&gt;
3449&lt;p&gt;The first reason is that &lt;code&gt;asyncio&lt;&#x2F;code&gt; makes your code easier to reason about, as opposed to using threads, because the amount of ways in which your code can run grows exponentially. Let&#x27;s see that with an example. Imagine you have this code:&lt;&#x2F;p&gt;
3450&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;def method():
3451	line 1
3452	line 2
3453	line 3
3454	line 4
3455	line 5
3456&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3457&lt;p&gt;And you start two threads to run the method at the same time. What is the order in which the lines of code get executed? The answer is that you can&#x27;t know! The first thread can run the entire method before the second thread even starts. Or it could be the first thread that runs after the second thread. Perhaps both run the &amp;quot;line 1&amp;quot;, and then the line 2. Maybe the first thread runs lines 1 and 2, and then the second thread only runs the line 1 before the first thread finishes.&lt;&#x2F;p&gt;
3458&lt;p&gt;As you can see, any combination of the order in which the lines run is possible. If the lines modify some global shared state, that will get messy quickly.&lt;&#x2F;p&gt;
3459&lt;p&gt;Second, in Python, threads &lt;em&gt;won&#x27;t&lt;&#x2F;em&gt; make your code faster most of the time. It will only increase the concurrency of your program (which is okay if it makes many blocking calls), allowing you to run several things at the same time.&lt;&#x2F;p&gt;
3460&lt;p&gt;If you have a lot of CPU work to do though, threads aren&#x27;t a real advantage. Indeed, your code will probably run slower under the most common Python implementation, CPython, which makes use of a Global Interpreter Lock (GIL) that only lets a thread run at once. The operations won&#x27;t run in parallel!&lt;&#x2F;p&gt;
3461&lt;h2 id=&quot;input-output&quot;&gt;Input &#x2F; Output&lt;&#x2F;h2&gt;
3462&lt;p&gt;Before we go any further, let&#x27;s first stop to talk about input and output, commonly known as &amp;quot;IO&amp;quot;. There are two main ways to perform IO operations, such as reading or writing from a file or a network socket.&lt;&#x2F;p&gt;
3463&lt;p&gt;The first one is known as &amp;quot;blocking IO&amp;quot;. What this means is that, when you try performing IO, the current application thread is going to &lt;em&gt;block&lt;&#x2F;em&gt; until the Operative System can tell you it&#x27;s done. Normally, this is not a problem, since disks are pretty fast anyway, but it can soon become a performance bottleneck. And network IO will be much slower than disk IO!&lt;&#x2F;p&gt;
3464&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;import socket
3465
3466# Setup a network socket and a very simple HTTP request.
3467# By default, sockets are open in blocking mode.
3468sock = socket.socket()
3469request = b&#x27;&#x27;&#x27;HEAD &#x2F; HTTP&#x2F;1.0\r
3470Host: example.com\r
3471\r
3472&#x27;&#x27;&#x27;
3473
3474# &amp;quot;connect&amp;quot; will block until a successful TCP connection
3475# is made to the host &amp;quot;example.com&amp;quot; on port 80.
3476sock.connect((&#x27;example.com&#x27;, 80))
3477
3478# &amp;quot;sendall&amp;quot; will repeatedly call &amp;quot;send&amp;quot; until all the data in &amp;quot;request&amp;quot; is
3479# sent to the host we just connected, which blocks until the data is sent.
3480sock.sendall(request)
3481
3482# &amp;quot;recv&amp;quot; will try to receive up to 1024 bytes from the host, and block until
3483# there is any data to receive (or empty if the host closes the connection).
3484response = sock.recv(1024)
3485
3486# After all those blocking calls, we got out data! These are the headers from
3487# making a HTTP request to example.com.
3488print(response.decode())
3489&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3490&lt;p&gt;Blocking IO offers timeouts, so that you can get control back in your code if the operation doesn&#x27;t finish. Imagine that the remote host doesn&#x27;t want to reply, your code would be stuck for as long as the connection remains alive!&lt;&#x2F;p&gt;
3491&lt;p&gt;But wait, what if we make the timeout small? Very, very small? If we do that, we will never block waiting for an answer. That&#x27;s how asynchronous IO works, and it&#x27;s the opposite of blocking IO (you can also call it non-blocking IO if you want to).&lt;&#x2F;p&gt;
3492&lt;p&gt;How does non-blocking IO work if the IO device needs a while to answer with the data? In that case, the operative system responds with &amp;quot;not ready&amp;quot;, and your application gets control back so it can do other stuff while the IO device completes your request. It works a bit like this:&lt;&#x2F;p&gt;
3493&lt;pre&gt;&lt;code&gt;&amp;lt;app&amp;gt; Hey, I would like to read 16 bytes from this file
3494&amp;lt;OS&amp;gt; Okay, but the disk hasn&#x27;t sent me the data yet
3495&amp;lt;app&amp;gt; Alright, I will do something else then
3496(a lot of computer time passes)
3497&amp;lt;app&amp;gt; Do you have my 16 bytes now?
3498&amp;lt;OS&amp;gt; Yes, here they are! &amp;quot;Hello, world !!\n&amp;quot;
3499&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3500&lt;p&gt;In reality, you can tell the OS to notify you when the data is ready, as opposed to polling (constantly asking the OS whether the data is ready yet or not), which is more efficient.&lt;&#x2F;p&gt;
3501&lt;p&gt;But either way, that&#x27;s the difference between blocking and non-blocking IO, and what matters is that your application gets to run more without ever needing to wait for data to arrive, because the data will be there immediately when you ask, and if it&#x27;s not yet, your app can do more things meanwhile.&lt;&#x2F;p&gt;
3502&lt;h2 id=&quot;diving-in&quot;&gt;Diving In&lt;&#x2F;h2&gt;
3503&lt;p&gt;Now we&#x27;ve seen what blocking and non-blocking IO is, and how threads make your code harder to reason about, but they give concurrency (yet not more speed). Is there any other way to achieve this concurrency that doesn&#x27;t involve threads? Yes! The answer is &lt;code&gt;asyncio&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
3504&lt;p&gt;So how does &lt;code&gt;asyncio&lt;&#x2F;code&gt; help? First we need to understand a very crucial concept before we can dive any deeper, and I&#x27;m talking about the &lt;em&gt;event loop&lt;&#x2F;em&gt;. What is it and why do we need it?&lt;&#x2F;p&gt;
3505&lt;p&gt;You can think of the event loop as a &lt;em&gt;loop&lt;&#x2F;em&gt; that will be responsible for calling your &lt;code&gt;async&lt;&#x2F;code&gt; functions:&lt;&#x2F;p&gt;
3506&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;asyncio&#x2F;eventloop.svg&quot; alt=&quot;The Event Loop&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
3507&lt;p&gt;That&#x27;s silly you may think. Now not only we run our code but we also have to run some &amp;quot;event loop&amp;quot;. It doesn&#x27;t sound beneficial at all. What are these events? Well, they are the IO events we talked about before!&lt;&#x2F;p&gt;
3508&lt;p&gt;&lt;code&gt;asyncio&lt;&#x2F;code&gt;&#x27;s event loop is responsible for handling those IO events, such as file is ready, data arrived, flushing is done, and so on. As we saw before, we can make these events non-blocking by setting their timeout to 0.&lt;&#x2F;p&gt;
3509&lt;p&gt;Let&#x27;s say you want to read from 10 files at the same time. You will ask the OS to read data from 10 files, and at first none of the reads will be ready. But the event loop will be constantly asking the OS to know which are done, and when they are done, you will get your data.&lt;&#x2F;p&gt;
3510&lt;p&gt;This has some nice advantages. It means that, instead of waiting for a network request to send you a response or some file, instead of blocking there, the event loop can decide to run other code meanwhile. Whenever the contents are ready, they can be read, and your code can continue. Waiting for the contents to be received is done with the &lt;code&gt;await&lt;&#x2F;code&gt; keyword, and it tells the loop that it can run other code meanwhile:&lt;&#x2F;p&gt;
3511&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;asyncio&#x2F;awaitkwd1.svg&quot; alt=&quot;Step 1, await keyword&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
3512&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;asyncio&#x2F;awaitkwd2.svg&quot; alt=&quot;Step 2, await keyword&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
3513&lt;p&gt;Start reading the code of the event loop and follow the arrows. You can see that, in the beginning, there are no events yet, so the loop calls one of your functions. The code runs until it has to &lt;code&gt;await&lt;&#x2F;code&gt; for some IO operation to complete, such as sending a request over the network. The method is &amp;quot;paused&amp;quot; until an event occurs (for example, an &amp;quot;event&amp;quot; occurs when the request has been sent completely).&lt;&#x2F;p&gt;
3514&lt;p&gt;While the first method is busy, the event loop can enter the second method, and run its code until the first &lt;code&gt;await&lt;&#x2F;code&gt;. But it can happen that the event of the second query occurs before the request on the first method, so the event loop can re-enter the second method because it has already sent the query, but the first method isn&#x27;t done sending the request yet.&lt;&#x2F;p&gt;
3515&lt;p&gt;Then, the second method &lt;code&gt;await&lt;&#x2F;code&gt;&#x27;s for an answer, and an event occurs telling the event loop that the request from the first method was sent. The code can be resumed again, until it has to &lt;code&gt;await&lt;&#x2F;code&gt; for a response, and so on. Here&#x27;s an explanation with pseudo-code for this process if you prefer:&lt;&#x2F;p&gt;
3516&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;async def method(request):
3517    prepare request
3518    await send request
3519
3520    await receive request
3521
3522    process request
3523    return result
3524
3525run in parallel (
3526	method with request 1,
3527	method with request 2,
3528)
3529&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3530&lt;p&gt;This is what the event loop will do on the above pseudo-code:&lt;&#x2F;p&gt;
3531&lt;pre&gt;&lt;code&gt;no events pending, can advance
3532
3533enter method with request 1
3534	prepare request
3535	await sending request
3536pause method with request 1
3537
3538no events ready, can advance
3539
3540enter method with request 2
3541	prepare request
3542	await sending request
3543pause method with request 2
3544
3545both requests are paused, cannot advance
3546wait for events
3547event for request 2 arrives (sending request completed)
3548
3549enter method with request 2
3550	await receiving response
3551pause method with request 2
3552
3553event for request 1 arrives (sending request completed)
3554
3555enter method with request 1
3556	await receiving response
3557pause method with request 1
3558
3559...and so on
3560&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3561&lt;p&gt;You may be wondering &amp;quot;okay, but threads work for me, so why should I change?&amp;quot;. There are some important things to note here. The first is that we only need one thread to be running! The event loop decides when and which methods should run. This results in less pressure for the operating system. The second is that we know when it may run other methods. Those are the &lt;code&gt;await&lt;&#x2F;code&gt; keywords! Whenever there is one of those, we know that the loop is able to run other things until the resource (again, like network) becomes ready (when a event occurs telling us it&#x27;s ready to be used without blocking or it has completed).&lt;&#x2F;p&gt;
3562&lt;p&gt;So far, we already have two advantages. We are only using a single thread so the cost for switching between methods is low, and we can easily reason about where our program may interleave operations.&lt;&#x2F;p&gt;
3563&lt;p&gt;Another advantage is that, with the event loop, you can easily schedule when a piece of code should run, such as using the method &lt;a href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;asyncio-eventloop.html#asyncio.loop.call_at&quot;&gt;&lt;code&gt;loop.call_at&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, without the need for spawning another thread at all.&lt;&#x2F;p&gt;
3564&lt;p&gt;To tell the &lt;code&gt;asyncio&lt;&#x2F;code&gt; to run the two methods shown above, we can use &lt;a href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;asyncio-future.html#asyncio.ensure_future&quot;&gt;&lt;code&gt;asyncio.ensure_future&lt;&#x2F;code&gt;&lt;&#x2F;a&gt;, which is a way of saying &amp;quot;I want the future of my method to be ensured&amp;quot;. That is, you want to run your method in the future, whenever the loop is free to do so. This method returns a &lt;code&gt;Future&lt;&#x2F;code&gt; object, so if your method returns a value, you can &lt;code&gt;await&lt;&#x2F;code&gt; this future to retrieve its result.&lt;&#x2F;p&gt;
3565&lt;p&gt;What is a &lt;code&gt;Future&lt;&#x2F;code&gt;? This object represents the value of something that will be there in the future, but might not be there yet. Just like you can &lt;code&gt;await&lt;&#x2F;code&gt; your own &lt;code&gt;async def&lt;&#x2F;code&gt; functions, you can &lt;code&gt;await&lt;&#x2F;code&gt; these &lt;code&gt;Future&lt;&#x2F;code&gt;&#x27;s.&lt;&#x2F;p&gt;
3566&lt;p&gt;The &lt;code&gt;async def&lt;&#x2F;code&gt; functions are also called &amp;quot;coroutines&amp;quot;, and Python does some magic behind the scenes to turn them into such. The coroutines can be &lt;code&gt;await&lt;&#x2F;code&gt;&#x27;ed, and this is what you normally do.&lt;&#x2F;p&gt;
3567&lt;h2 id=&quot;a-toy-example&quot;&gt;A Toy Example&lt;&#x2F;h2&gt;
3568&lt;p&gt;That&#x27;s all about &lt;code&gt;asyncio&lt;&#x2F;code&gt;! Let&#x27;s wrap up with some example code. We will create a server that replies with the text a client sends, but reversed. First, we will show what you could write with normal synchronous code, and then we will port it.&lt;&#x2F;p&gt;
3569&lt;p&gt;Here is the &lt;strong&gt;synchronous version&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
3570&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;# server.py
3571import socket
3572
3573
3574def server_method():
3575	# create a new server socket to listen for connections
3576	server = socket.socket()
3577
3578	# bind to localhost:6789 for new connections
3579	server.bind((&#x27;localhost&#x27;, 6789))
3580
3581	# we will listen for one client at most
3582	server.listen(1)
3583
3584	# *block* waiting for a new client
3585	client, _ = server.accept()
3586
3587	# *block* waiting for some data
3588	data = client.recv(1024)
3589
3590	# reverse the data
3591	data = data[::-1]
3592
3593	# *block* sending the data
3594	client.sendall(data)
3595
3596	# close client and server
3597	server.close()
3598	client.close()
3599
3600
3601if __name__ == &#x27;__main__&#x27;:
3602	# block running the server
3603	server_method()
3604&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3605&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;# client.py
3606import socket
3607
3608
3609def client_method():
3610	message = b&#x27;Hello Server!\n&#x27;
3611	client = socket.socket()
3612
3613	# *block* trying to stabilish a connection
3614	client.connect((&#x27;localhost&#x27;, 6789))
3615
3616	# *block* trying to send the message
3617	print(&#x27;Sending&#x27;, message)
3618	client.sendall(message)
3619
3620	# *block* until we receive a response
3621	response = client.recv(1024)
3622	print(&#x27;Server replied&#x27;, response)
3623
3624	client.close()
3625
3626
3627if __name__ == &#x27;__main__&#x27;:
3628	client_method()
3629&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3630&lt;p&gt;From what we&#x27;ve seen, this code will block on all the lines with a comment above them saying that they will block. This means that for running more than one client or server, or both in the same file, you will need threads. But we can do better, we can rewrite it into &lt;code&gt;asyncio&lt;&#x2F;code&gt;!&lt;&#x2F;p&gt;
3631&lt;p&gt;The first step is to mark all your &lt;code&gt;def&lt;&#x2F;code&gt;initions that may block with &lt;code&gt;async&lt;&#x2F;code&gt;. This marks them as coroutines, which can be &lt;code&gt;await&lt;&#x2F;code&gt;ed on.&lt;&#x2F;p&gt;
3632&lt;p&gt;Second, since we&#x27;re using low-level sockets, we need to make use of the methods that &lt;code&gt;asyncio&lt;&#x2F;code&gt; provides directly. If this was a third-party library, this would be just like using their &lt;code&gt;async def&lt;&#x2F;code&gt;initions.&lt;&#x2F;p&gt;
3633&lt;p&gt;Here is the &lt;strong&gt;asynchronous version&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
3634&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;# server.py
3635import asyncio
3636import socket
3637
3638# get the default &amp;quot;event loop&amp;quot; that we will run
3639loop = asyncio.get_event_loop()
3640
3641
3642# notice our new &amp;quot;async&amp;quot; before the definition
3643async def server_method():
3644	server = socket.socket()
3645	server.bind((&#x27;localhost&#x27;, 6789))
3646	server.listen(1)
3647
3648	# await for a new client
3649	# the event loop can run other code while we wait here!
3650	client, _ = await loop.sock_accept(server)
3651
3652	# await for some data
3653	data = await loop.sock_recv(client, 1024)
3654	data = data[::-1]
3655
3656	# await for sending the data
3657	await loop.sock_sendall(client, data)
3658
3659	server.close()
3660	client.close()
3661
3662
3663if __name__ == &#x27;__main__&#x27;:
3664	# run the loop until &amp;quot;server method&amp;quot; is complete
3665	loop.run_until_complete(server_method())
3666&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3667&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;# client.py
3668import asyncio
3669import socket
3670
3671loop = asyncio.get_event_loop()
3672
3673
3674async def client_method():
3675	message = b&#x27;Hello Server!\n&#x27;
3676	client = socket.socket()
3677
3678	# await to stabilish a connection
3679	await loop.sock_connect(client, (&#x27;localhost&#x27;, 6789))
3680
3681	# await to send the message
3682	print(&#x27;Sending&#x27;, message)
3683	await loop.sock_sendall(client, message)
3684
3685	# await to receive a response
3686	response = await loop.sock_recv(client, 1024)
3687	print(&#x27;Server replied&#x27;, response)
3688
3689	client.close()
3690
3691
3692if __name__ == &#x27;__main__&#x27;:
3693	loop.run_until_complete(client_method())
3694&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3695&lt;p&gt;That&#x27;s it! You can place these two files separately and run, first the server, then the client. You should see output in the client.&lt;&#x2F;p&gt;
3696&lt;p&gt;The big difference here is that you can easily modify the code to run more than one server or clients at the same time. Whenever you &lt;code&gt;await&lt;&#x2F;code&gt; the event loop will run other of your code. It seems to &amp;quot;block&amp;quot; on the &lt;code&gt;await&lt;&#x2F;code&gt; parts, but remember it&#x27;s actually jumping to run more code, and the event loop will get back to you whenever it can.&lt;&#x2F;p&gt;
3697&lt;p&gt;In short, you need an &lt;code&gt;async def&lt;&#x2F;code&gt; to &lt;code&gt;await&lt;&#x2F;code&gt; things, and you run them with the event loop instead of calling them directly. So this…&lt;&#x2F;p&gt;
3698&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;def main():
3699	...  # some code
3700
3701
3702if __name__ == &#x27;__main__&#x27;:
3703	main()
3704&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3705&lt;p&gt;…becomes this:&lt;&#x2F;p&gt;
3706&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;import asyncio
3707
3708
3709async def main():
3710	...  # some code
3711
3712
3713if __name__ == &#x27;__main__&#x27;:
3714	asyncio.get_event_loop().run_until_complete(main)
3715&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3716&lt;p&gt;This is pretty much how most of your &lt;code&gt;async&lt;&#x2F;code&gt; scripts will start, running the main method until its completion.&lt;&#x2F;p&gt;
3717&lt;h2 id=&quot;a-real-example&quot;&gt;A Real Example&lt;&#x2F;h2&gt;
3718&lt;p&gt;Let&#x27;s have some fun with a real library. We&#x27;ll be using &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;LonamiWebs&#x2F;Telethon&quot;&gt;Telethon&lt;&#x2F;a&gt; to broadcast a message to our three best friends, all at the same time, thanks to the magic of &lt;code&gt;asyncio&lt;&#x2F;code&gt;. We&#x27;ll dive right into the code, and then I&#x27;ll explain our new friend &lt;code&gt;asyncio.wait(...)&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
3719&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;# broadcast.py
3720import asyncio
3721import sys
3722
3723from telethon import TelegramClient
3724
3725# (you need your own values here, check Telethon&#x27;s documentation)
3726api_id = 123
3727api_hash = &#x27;123abc&#x27;
3728friends = [
3729	&#x27;@friend1__username&#x27;,
3730	&#x27;@friend2__username&#x27;,
3731	&#x27;@bestie__username&#x27;
3732]
3733
3734# we will have to await things, so we need an async def
3735async def main(message):
3736	# start is a coroutine, so we need to await it to run it
3737	client = await TelegramClient(&#x27;me&#x27;, api_id, api_hash).start()
3738
3739	# wait for all three client.send_message to complete
3740	await asyncio.wait([
3741		client.send_message(friend, message)
3742		for friend in friends
3743	])
3744
3745	# and close our client
3746	await client.disconnect()
3747
3748
3749if __name__ == &#x27;__main__&#x27;:
3750	if len(sys.argv) != 2:
3751		print(&#x27;You must pass the message to broadcast!&#x27;)
3752		quit()
3753
3754	message = sys.argv[1]
3755	asyncio.get_event_loop().run_until_complete(main(message))
3756&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3757&lt;p&gt;Wait… how did that send a message to all three of
3758my friends? The magic is done here:&lt;&#x2F;p&gt;
3759&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;[
3760	client.send_message(friend, message)
3761	for friend in friends
3762]
3763&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3764&lt;p&gt;This list comprehension creates another list with three
3765coroutines, the three &lt;code&gt;client.send_message(...)&lt;&#x2F;code&gt;.
3766Then we just pass that list to &lt;code&gt;asyncio.wait&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
3767&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;await asyncio.wait([...])
3768&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
3769&lt;p&gt;This method, by default, waits for the list of coroutines to run until they&#x27;ve all finished. You can read more on the Python &lt;a href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;asyncio-task.html#asyncio.wait&quot;&gt;documentation&lt;&#x2F;a&gt;. Truly a good function to know about!&lt;&#x2F;p&gt;
3770&lt;p&gt;Now whenever you have some important news for your friends, you can simply &lt;code&gt;python3 broadcast.py &#x27;I bought a car!&#x27;&lt;&#x2F;code&gt; to tell all your friends about your new car! All you need to remember is that you need to &lt;code&gt;await&lt;&#x2F;code&gt; on coroutines, and you will be good. &lt;code&gt;asyncio&lt;&#x2F;code&gt; will warn you when you forget to do so.&lt;&#x2F;p&gt;
3771&lt;h2 id=&quot;extra-material&quot;&gt;Extra Material&lt;&#x2F;h2&gt;
3772&lt;p&gt;If you want to understand how &lt;code&gt;asyncio&lt;&#x2F;code&gt; works under the hood, I recommend you to watch this hour-long talk &lt;a href=&quot;https:&#x2F;&#x2F;youtu.be&#x2F;M-UcUs7IMIM&quot;&gt;Get to grips with asyncio in Python 3&lt;&#x2F;a&gt; by Robert Smallshire. In the video, they will explain the differences between concurrency and parallelism, along with others concepts, and how to implement your own &lt;code&gt;asyncio&lt;&#x2F;code&gt; &amp;quot;scheduler&amp;quot; from scratch.&lt;&#x2F;p&gt;
3773</content>
3774	</entry>
3775	<entry xml:lang="en">
3776		<title>Atemporal Blog Posts</title>
3777		<published>2018-02-03T00:00:00+00:00</published>
3778		<updated>2021-02-19T00:00:00+00:00</updated>
3779		<link href="https://lonami.dev/blog/posts/" type="text/html"/>
3780		<id>https://lonami.dev/blog/posts/</id>
3781		<content type="html">&lt;p&gt;These are some interesting posts and links I&#x27;ve found around the web. I believe they are quite interesting and nice reads, so if you have the time, I encourage you to check some out.&lt;&#x2F;p&gt;
3782&lt;h2 id=&quot;algorithms&quot;&gt;Algorithms&lt;&#x2F;h2&gt;
3783&lt;ul&gt;
3784&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.tannerhelland.com&#x2F;4660&#x2F;dithering-eleven-algorithms-source-code&#x2F;&quot;&gt;Image Dithering: Eleven Algorithms and Source Code&lt;&#x2F;a&gt;. What does it mean and how to achieve it?&lt;&#x2F;li&gt;
3785&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;cristian.io&#x2F;post&#x2F;bloom-filters&#x2F;&quot;&gt;Idempotence layer on bloom filters&lt;&#x2F;a&gt;. What are they and how can they help?&lt;&#x2F;li&gt;
3786&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Huffman_coding&quot;&gt;Huffman coding&lt;&#x2F;a&gt;. This encoding is a simple yet interesting way of compressing information.&lt;&#x2F;li&gt;
3787&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;mxgmn&#x2F;WaveFunctionCollapse&quot;&gt;Wave Function Collapse&lt;&#x2F;a&gt;. Bitmap &amp;amp; tilemap generation from a single example with the help of ideas from quantum mechanics.&lt;&#x2F;li&gt;
3788&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blog.nelhage.com&#x2F;2015&#x2F;02&#x2F;regular-expression-search-with-suffix-arrays&#x2F;&quot;&gt;Regular Expression Search with Suffix Arrays&lt;&#x2F;a&gt;. A way to efficiently search large amounts of text.&lt;&#x2F;li&gt;
3789&lt;&#x2F;ul&gt;
3790&lt;h2 id=&quot;culture&quot;&gt;Culture&lt;&#x2F;h2&gt;
3791&lt;ul&gt;
3792&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.wired.com&#x2F;story&#x2F;ideas-joi-ito-robot-overlords&#x2F;&quot;&gt;Why Westerners Fear Robots and the Japanese Do Not&lt;&#x2F;a&gt;. Explains some possible reasons for this case.&lt;&#x2F;li&gt;
3793&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;catb.org&#x2F;%7Eesr&#x2F;faqs&#x2F;smart-questions.html&quot;&gt;How To Ask Questions The Smart Way&lt;&#x2F;a&gt;. Some bits of hacker culture and amazing tips on how to ask a question.&lt;&#x2F;li&gt;
3794&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;apenwarr.ca&#x2F;log&#x2F;?m=201809#14&quot;&gt;XML, blockchains, and the strange shapes of progress&lt;&#x2F;a&gt;. Some of history about XML and blockchain.&lt;&#x2F;li&gt;
3795&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;czep.net&#x2F;17&#x2F;legion-of-lobotomized-unices.html&quot;&gt;Legion of lobotomized unices&lt;&#x2F;a&gt;. A time where computers are treated a lot more nicely.&lt;&#x2F;li&gt;
3796&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;eli.thegreenplace.net&#x2F;2016&#x2F;the-expression-problem-and-its-solutions&#x2F;&quot;&gt;The Expression Problem and its solutions&lt;&#x2F;a&gt;. What is it and what can we do to solve it?&lt;&#x2F;li&gt;
3797&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;allendowney.blogspot.com&#x2F;2015&#x2F;08&#x2F;the-inspection-paradox-is-everywhere.html&quot;&gt;The Inspection Paradox is Everywhere&lt;&#x2F;a&gt;. Interesting and very common phenomena.&lt;&#x2F;li&gt;
3798&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;ChrisKnott&#x2F;Algojammer&quot;&gt;An experimental code editor for writing algorithms&lt;&#x2F;a&gt;. Contains several links to different tools for reverse debugging.&lt;&#x2F;li&gt;
3799&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;habitatchronicles.com&#x2F;2017&#x2F;05&#x2F;what-are-capabilities&#x2F;&quot;&gt;What Are Capabilities?&lt;&#x2F;a&gt; Good ideas with great security implications.&lt;&#x2F;li&gt;
3800&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blog.aurynn.com&#x2F;2015&#x2F;12&#x2F;16-contempt-culture&quot;&gt;Contempt Culture&lt;&#x2F;a&gt;. Or why you should not speak crap about your non-favourite programming languages.&lt;&#x2F;li&gt;
3801&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.lesswrong.com&#x2F;posts&#x2F;tscc3e5eujrsEeFN4&#x2F;well-kept-gardens-die-by-pacifism&quot;&gt;Well-Kept Gardens Die By Pacifism&lt;&#x2F;a&gt;. Risks any online community can run into.&lt;&#x2F;li&gt;
3802&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;ncase.me&#x2F;&quot;&gt;It&#x27;s Nicky Case!&lt;&#x2F;a&gt; They make some cool things worth checking out, I really like &amp;quot;we become what we behold&amp;quot;.&lt;&#x2F;li&gt;
3803&lt;&#x2F;ul&gt;
3804&lt;h2 id=&quot;debate&quot;&gt;Debate&lt;&#x2F;h2&gt;
3805&lt;ul&gt;
3806&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;steemit.com&#x2F;opensource&#x2F;@crell&#x2F;open-source-is-awful&quot;&gt;Open Source is awful&lt;&#x2F;a&gt;. Has some points about why is it bad and how it could improve.&lt;&#x2F;li&gt;
3807&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.mondo2000.com&#x2F;2018&#x2F;01&#x2F;17&#x2F;pink-lexical-goop-dark-side-autocorrect&#x2F;&quot;&gt;Pink Lexical Goop: The Dark Side of Autocorrect&lt;&#x2F;a&gt;. It can shape how you think.&lt;&#x2F;li&gt;
3808&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;blog.ploeh.dk&#x2F;2015&#x2F;08&#x2F;03&#x2F;idiomatic-or-idiosyncratic&#x2F;&quot;&gt;Idiomatic or idiosyncratic?&lt;&#x2F;a&gt; Can porting code constructs from other languages have a positive effect?&lt;&#x2F;li&gt;
3809&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;gamasutra.com&#x2F;view&#x2F;news&#x2F;169296&#x2F;Indepth_Functional_programming_in_C.php&quot;&gt;In-depth: Functional programming in C++&lt;&#x2F;a&gt;. Is it useful to bother with functional concepts in a language like C++?&lt;&#x2F;li&gt;
3810&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;vorpus.org&#x2F;blog&#x2F;notes-on-structured-concurrency-or-go-statement-considered-harmful&#x2F;&quot;&gt;Notes on structured concurrency, or: Go statement considered harmful&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
3811&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;queue.acm.org&#x2F;detail.cfm?id=3212479&quot;&gt;C Is Not a Low-level Language&lt;&#x2F;a&gt;. Could there be alternative programming models designed for more specialized CPUs?&lt;&#x2F;li&gt;
3812&lt;&#x2F;ul&gt;
3813&lt;h2 id=&quot;food-for-thought&quot;&gt;Food for Thought&lt;&#x2F;h2&gt;
3814&lt;ul&gt;
3815&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.hillelwayne.com&#x2F;post&#x2F;divide-by-zero&#x2F;&quot;&gt;1&#x2F;0 = 0&lt;&#x2F;a&gt;. Explores why it makes sense to redefine mathemathics under some circumstances, and why it is possible to do so.&lt;&#x2F;li&gt;
3816&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;jeremykun.com&#x2F;2018&#x2F;04&#x2F;13&#x2F;for-mathematicians-does-not-mean-equality&#x2F;&quot;&gt;For mathematicians, = does not mean equality&lt;&#x2F;a&gt;. What other definitions does the equal sign have?&lt;&#x2F;li&gt;
3817&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.lesswrong.com&#x2F;posts&#x2F;2MD3NMLBPCqPfnfre&#x2F;cached-thoughts&quot;&gt;Cached Thoughts&lt;&#x2F;a&gt;. How is it possible that our brains work at all?&lt;&#x2F;li&gt;
3818&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;tonsky.me&#x2F;blog&#x2F;disenchantment&#x2F;&quot;&gt;Software disenchantment&lt;&#x2F;a&gt;. Faster hardware and slower software is a trend.
3819&lt;ul&gt;
3820&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;blackhole12.com&#x2F;blog&#x2F;software-engineering-is-bad-but-it-s-not-that-bad&#x2F;&quot;&gt;Software Engineering Is Bad, But That&#x27;s Not Why&lt;&#x2F;a&gt;. This post has some good counterpoints to Software disenchantment.&lt;&#x2F;li&gt;
3821&lt;&#x2F;ul&gt;
3822&lt;&#x2F;li&gt;
3823&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;journal.stuffwithstuff.com&#x2F;2015&#x2F;02&#x2F;01&#x2F;what-color-is-your-function&#x2F;&quot;&gt;What Color is Your Function?&lt;&#x2F;a&gt;. Spoiler: can we approach asynchronous IO better?&lt;&#x2F;li&gt;
3824&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;hackernoon.com&#x2F;im-harvesting-credit-card-numbers-and-passwords-from-your-site-here-s-how-9a8cb347c5b5&quot;&gt;I&#x27;m harvesting credit card numbers and passwords from your site&lt;&#x2F;a&gt;. A word of warning when mindlessly adding dependencies.&lt;&#x2F;li&gt;
3825&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;medium.com&#x2F;message&#x2F;everything-is-broken-81e5f33a24e1&quot;&gt;Everything Is Broken&lt;&#x2F;a&gt;. Some of the (probable) truths about our world.&lt;&#x2F;li&gt;
3826&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;johnsalvatier.org&#x2F;blog&#x2F;2017&#x2F;reality-has-a-surprising-amount-of-detail&quot;&gt;Reality has a surprising amount of detail&lt;&#x2F;a&gt;.&lt;&#x2F;li&gt;
3827&lt;&#x2F;ul&gt;
3828&lt;h2 id=&quot;funny&quot;&gt;Funny&lt;&#x2F;h2&gt;
3829&lt;ul&gt;
3830&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;thedailywtf.com&#x2F;articles&#x2F;We-Use-BobX&quot;&gt;We Use BobX&lt;&#x2F;a&gt;. BobX.&lt;&#x2F;li&gt;
3831&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;thedailywtf.com&#x2F;articles&#x2F;the-inner-json-effect&quot;&gt;The Inner JSON Effect&lt;&#x2F;a&gt;. For some reason, custom languages are in.&lt;&#x2F;li&gt;
3832&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;thedailywtf.com&#x2F;articles&#x2F;exponential-backup&quot;&gt;Exponential Backup&lt;&#x2F;a&gt;. Far better than git.&lt;&#x2F;li&gt;
3833&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;thedailywtf.com&#x2F;articles&#x2F;ITAPPMONROBOT&quot;&gt;ITAPPMONROBOT&lt;&#x2F;a&gt;. Solving software problems with hardware.&lt;&#x2F;li&gt;
3834&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;thedailywtf.com&#x2F;articles&#x2F;a-tapestry-of-threads&quot;&gt;A Tapestry of Threads&lt;&#x2F;a&gt;. More threads must mean faster code, right?&lt;&#x2F;li&gt;
3835&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;medium.com&#x2F;commitlog&#x2F;a-brief-totally-accurate-history-of-programming-languages-cd93ec806124&quot;&gt;A Brief Totally Accurate History Of Programming Languages&lt;&#x2F;a&gt;. Don&#x27;t take offense for it!&lt;&#x2F;li&gt;
3836&lt;&#x2F;ul&gt;
3837&lt;h2 id=&quot;graphics&quot;&gt;Graphics&lt;&#x2F;h2&gt;
3838&lt;ul&gt;
3839&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;shaunlebron.github.io&#x2F;visualizing-projections&#x2F;&quot;&gt;Visualizing Projections&lt;&#x2F;a&gt;. Small post about different projection methods.&lt;&#x2F;li&gt;
3840&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;www.iquilezles.org&#x2F;www&#x2F;index.htm&quot;&gt;Inigo Quilez :: fractals, computer graphics, mathematics, shaders, demoscene and more&lt;&#x2F;a&gt; A &lt;em&gt;lot&lt;&#x2F;em&gt; of useful and quality articles regarding computer graphics.&lt;&#x2F;li&gt;
3841&lt;&#x2F;ul&gt;
3842&lt;h2 id=&quot;history&quot;&gt;History&lt;&#x2F;h2&gt;
3843&lt;ul&gt;
3844&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;twobithistory.org&#x2F;2018&#x2F;08&#x2F;18&#x2F;ada-lovelace-note-g.html&quot;&gt;What Did Ada Lovelace&#x27;s Program Actually Do?&lt;&#x2F;a&gt;. And other characters that took part in the beginning&#x27;s of programming.&lt;&#x2F;li&gt;
3845&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;chrisdown.name&#x2F;2018&#x2F;01&#x2F;02&#x2F;in-defence-of-swap.html&quot;&gt;In defence of swap: common misconceptions&lt;&#x2F;a&gt;. Swap is still an useful concept.&lt;&#x2F;li&gt;
3846&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.pacifict.com&#x2F;Story&#x2F;&quot;&gt;The Graphing Calculator Story&lt;&#x2F;a&gt;. A great classic Apple tale.&lt;&#x2F;li&gt;
3847&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;twobithistory.org&#x2F;2018&#x2F;10&#x2F;14&#x2F;lisp.html&quot;&gt;How Lisp Became God&#x27;s Own Programming Language&lt;&#x2F;a&gt;. Lisp as a foundational programming language.&lt;&#x2F;li&gt;
3848&lt;&#x2F;ul&gt;
3849&lt;h2 id=&quot;motivational&quot;&gt;Motivational&lt;&#x2F;h2&gt;
3850&lt;ul&gt;
3851&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.joelonsoftware.com&#x2F;2002&#x2F;01&#x2F;06&#x2F;fire-and-motion&#x2F;&quot;&gt;Fire And Motion&lt;&#x2F;a&gt;. What does actually take to get things done?&lt;&#x2F;li&gt;
3852&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;realmensch.org&#x2F;2017&#x2F;08&#x2F;25&#x2F;the-parable-of-the-two-programmers&#x2F;&quot;&gt;The Parable of the Two Programmers&lt;&#x2F;a&gt;. This tale is about two different types of programmer and their respective endings in a company, illustrating how the one you wouldn&#x27;t expect to actually ends in a better situation.&lt;&#x2F;li&gt;
3853&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;byorgey.wordpress.com&#x2F;2018&#x2F;05&#x2F;06&#x2F;conversations-with-a-six-year-old-on-functional-programming&#x2F;&quot;&gt;Conversations with a six-year-old on functional programming&lt;&#x2F;a&gt;. Little kids today can be really interested in technological topics.&lt;&#x2F;li&gt;
3854&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;bulletproofmusician.com&#x2F;how-many-hours-a-day-should-you-practice&#x2F;&quot;&gt;How Many Hours a Day Should You Practice?&lt;&#x2F;a&gt;. While the article is about music, it applies to any other areas.&lt;&#x2F;li&gt;
3855&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;nathanmarz.com&#x2F;blog&#x2F;suffering-oriented-programming.html&quot;&gt;Suffering-oriented programming&lt;&#x2F;a&gt;. A possibly new approach on how you could tackle your new projects.&lt;&#x2F;li&gt;
3856&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.joelonsoftware.com&#x2F;2000&#x2F;04&#x2F;06&#x2F;things-you-should-never-do-part-i&#x2F;&quot;&gt;Things You Should Never Do, Part I&lt;&#x2F;a&gt;. There is no need to rewrite your code.&lt;&#x2F;li&gt;
3857&lt;&#x2F;ul&gt;
3858&lt;h2 id=&quot;optimization&quot;&gt;Optimization&lt;&#x2F;h2&gt;
3859&lt;ul&gt;
3860&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;blog.llvm.org&#x2F;2011&#x2F;05&#x2F;what-every-c-programmer-should-know.html&quot;&gt;What Every C Programmer Should Know About Undefined Behavior #1&#x2F;3&lt;&#x2F;a&gt;. Explains what undefined behaviour is and why it makes sense.&lt;&#x2F;li&gt;
3861&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;ridiculousfish.com&#x2F;blog&#x2F;posts&#x2F;labor-of-division-episode-i.html&quot;&gt;Labor of Division (Episode I)&lt;&#x2F;a&gt;. Some tricks to divide without division.&lt;&#x2F;li&gt;
3862&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;blog.moertel.com&#x2F;posts&#x2F;2013-12-14-great-old-timey-game-programming-hack.html&quot;&gt;A Great Old-Timey Game-Programming Hack&lt;&#x2F;a&gt;. Abusing instructions to make games playable even on the slowest hardware.&lt;&#x2F;li&gt;
3863&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20191213224640&#x2F;https:&#x2F;&#x2F;people.eecs.berkeley.edu&#x2F;%7Esangjin&#x2F;2012&#x2F;12&#x2F;21&#x2F;epoll-vs-kqueue.html&quot;&gt;Scalable Event Multiplexing: epoll vs kqueue&lt;&#x2F;a&gt;. How good OS primitives can really help performance and scability.&lt;&#x2F;li&gt;
3864&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;adamdrake.com&#x2F;command-line-tools-can-be-235x-faster-than-your-hadoop-cluster.html&quot;&gt;Command-line Tools can be 235x Faster than your Hadoop Cluster&lt;&#x2F;a&gt;. Or how to use the right tool for the right job.&lt;&#x2F;li&gt;
3865&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;nullprogram.com&#x2F;blog&#x2F;2018&#x2F;05&#x2F;27&#x2F;&quot;&gt;When FFI Function Calls Beat Native C&lt;&#x2F;a&gt;. How lua beat C at it and the explanation behind it.&lt;&#x2F;li&gt;
3866&lt;li&gt;&lt;a href=&quot;http:&#x2F;&#x2F;igoro.com&#x2F;archive&#x2F;gallery-of-processor-cache-effects&#x2F;&quot;&gt;Gallery of Processor Cache Effects&lt;&#x2F;a&gt;. Knowing a few things about the cache can make a big difference.&lt;&#x2F;li&gt;
3867&lt;&#x2F;ul&gt;
3868</content>
3869	</entry>
3870	<entry xml:lang="en">
3871		<title>Graphs</title>
3872		<published>2017-06-02T00:00:00+00:00</published>
3873		<updated>2017-06-02T00:00:00+00:00</updated>
3874		<link href="https://lonami.dev/blog/graphs/" type="text/html"/>
3875		<id>https://lonami.dev/blog/graphs/</id>
3876		<content type="html">&lt;p&gt;&lt;noscript&gt;There are a few things which won&#x27;t render unless you enable
3877JavaScript. No tracking, I promise!&lt;&#x2F;noscript&gt;&lt;&#x2F;p&gt;
3878&lt;blockquote&gt;
3879&lt;p&gt;Don&#x27;t know English? &lt;a href=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;graphs&#x2F;spanish.html&quot;&gt;Read the Spanish version instead&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
3880&lt;&#x2F;blockquote&gt;
3881&lt;p&gt;Let&#x27;s imagine we have 5 bus stations, which we&#x27;ll denote by ((s_i)):&lt;&#x2F;p&gt;
3882&lt;div class=&quot;matrix&quot;&gt;
3883      &#x27; s_1 &#x27; s_2 &#x27; s_3 &#x27; s_4 &#x27; s_5 \\
3884s_1   &#x27;     &#x27;  V  &#x27;     &#x27;     &#x27;     \\
3885s_2   &#x27;  V  &#x27;     &#x27;     &#x27;     &#x27;  V  \\
3886s_3   &#x27;     &#x27;     &#x27;     &#x27;  V  &#x27;     \\
3887s_4   &#x27;     &#x27;  V  &#x27;  V  &#x27;     &#x27;     \\
3888s_5   &#x27;  V  &#x27;     &#x27;     &#x27;  V  &#x27;
3889&lt;&#x2F;div&gt;
3890&lt;p&gt;This is known as a &amp;quot;table of direct interconnections&amp;quot;.
3891The ((V)) represent connected paths. For instance, on the first
3892row starting at ((s_1)), reaching the ((V)),
3893allows us to turn up to get to ((s_2)).&lt;&#x2F;p&gt;
3894&lt;p&gt;We can see the above table represented in a more graphical way:&lt;&#x2F;p&gt;
3895&lt;p&gt;&lt;img src=&quot;https:&#x2F;&#x2F;lonami.dev&#x2F;blog&#x2F;graphs&#x2F;example1.svg&quot; alt=&quot;Table 1 as a Graph&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
3896&lt;p&gt;This type of graph is called, well, a graph, and it&#x27;s a directed
3897graph (or digraph), since the direction on which the arrows go does
3898matter. It&#x27;s made up of vertices, joined together by edges (also known as
3899lines or directed arcs).&lt;&#x2F;p&gt;
3900&lt;p&gt;One can walk from a node to another through different paths. For
3901example, ((s_4 $rightarrow s_2 $rightarrow s_5)) is an indirect path of order
3902two, because we must use two edges to go from ((s_4)) to
3903((s_5)).&lt;&#x2F;p&gt;
3904&lt;p&gt;Let&#x27;s now represent its adjacency matrix called A which represents the
3905same table, but uses 1 instead V to represent
3906a connection:&lt;&#x2F;p&gt;
3907&lt;div class=&quot;matrix&quot;&gt;
3908  0 &#x27; 1 &#x27; 0 &#x27; 0 &#x27; 0 \\
3909  1 &#x27; 0 &#x27; 0 &#x27; 0 &#x27; 1 \\
3910  0 &#x27; 0 &#x27; 0 &#x27; 1 &#x27; 0 \\
3911  0 &#x27; 1 &#x27; 1 &#x27; 0 &#x27; 0 \\
3912  1 &#x27; 0 &#x27; 0 &#x27; 1 &#x27; 0
3913&lt;&#x2F;div&gt;
3914&lt;p&gt;This way we can see how the ((a_{2,1})) element represents the
3915connection ((s_2 $rightarrow s_1)), and the ((a_{5,1})) element the
3916((s_5 $rightarrow s_1)) connection, etc.&lt;&#x2F;p&gt;
3917&lt;p&gt;In general, ((a_{i,j})) represents a connection from
3918((s_i $rightarrow s_j))as long as ((a_{i,j}$geq 1)).&lt;&#x2F;p&gt;
3919&lt;p&gt;Working with matrices allows us to have a computable representation of
3920any graph, which is very useful.&lt;&#x2F;p&gt;
3921&lt;hr &#x2F;&gt;
3922&lt;p&gt;Graphs have a lot of interesting properties besides being representable
3923by a computer. What would happen if, for instance, we calculated
3924((A^2))? We obtain the following matrix:&lt;&#x2F;p&gt;
3925&lt;div class=&quot;matrix&quot;&gt;
39261 &#x27; 0 &#x27; 0 &#x27; 0 &#x27; 1 \\
39271 &#x27; 1 &#x27; 0 &#x27; 1 &#x27; 0 \\
39280 &#x27; 1 &#x27; 1 &#x27; 0 &#x27; 0 \\
39291 &#x27; 0 &#x27; 0 &#x27; 1 &#x27; 1 \\
39300 &#x27; 2 &#x27; 1 &#x27; 0 &#x27; 0
3931&lt;&#x2F;div&gt;
3932&lt;p&gt;We can interpret this as the paths of order two.
3933But what does the element ((a_{5,2}=2)) represent? It indicates
3934the amount of possible ways to go from  ((s_5 $rightarrow s_i $rightarrow s_2)).&lt;&#x2F;p&gt;
3935&lt;p&gt;One can manually multiply the involved row and column to determine which
3936element is the one we need to pass through, this way we have the row
3937(([1 0 0 1 0])) and the column (([1 0 0 1 0])) (on
3938vertical). The elements ((s_i$geq 1)) are ((s_1)) and
3939((s_4)). This is, we can go from ((s_5)) to
3940((s_2)) via ((s_5 $rightarrow s_1 $rightarrow s_2)) or via
3941((s_5 $rightarrow s_4 $rightarrow s_2)):
3942&lt;img src=&quot;example2.svg&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
3943&lt;p&gt;It&#x27;s important to note that graphs to not consider self-connections, this
3944is, ((s_i $rightarrow s_i)) is not allowed; neither we work with multigraphs
3945here (those which allow multiple connections, for instance, an arbitrary
3946number ((n)) of times).&lt;&#x2F;p&gt;
3947&lt;div class=&quot;matrix&quot;&gt;
39481 &#x27; 1 &#x27; 0          &#x27; 1 &#x27; 0 \\
39491 &#x27; 2 &#x27; \textbf{1} &#x27; 0 &#x27; 1 \\
39501 &#x27; 0 &#x27; 0          &#x27; 1 &#x27; 1 \\
39511 &#x27; 2 &#x27; 1          &#x27; 1 &#x27; 0 \\
39522 &#x27; 0 &#x27; 0          &#x27; 1 &#x27; 2
3953&lt;&#x2F;div&gt;
3954&lt;p&gt;We can see how the first ((1)) just appeared on the element
3955((a_{2,3})), which means that the shortest path to it is at least
3956of order three.&lt;&#x2F;p&gt;
3957&lt;hr &#x2F;&gt;
3958&lt;p&gt;A graph is said to be strongly connected as long as there is a
3959way to reach all its elements.&lt;&#x2F;p&gt;
3960&lt;p&gt;We can see all the available paths until now by simply adding up all the
3961direct and indirect ways to reach a node, so for now, we can add
3962((A+A^2+A^3)) in such a way that:&lt;&#x2F;p&gt;
3963&lt;div class=&quot;matrix&quot;&gt;
39642 &#x27; 2 &#x27; 0 &#x27; 1 &#x27; 1 \\
39653 &#x27; 3 &#x27; 1 &#x27; 1 &#x27; 3 \\
39661 &#x27; 1 &#x27; 1 &#x27; 2 &#x27; 1 \\
39672 &#x27; 3 &#x27; 2 &#x27; 2 &#x27; 1 \\
39683 &#x27; 2 &#x27; 1 &#x27; 2 &#x27; 2
3969&lt;&#x2F;div&gt;
3970&lt;p&gt;There isn&#x27;t a connection between ((s_1)) and ((s_3)) yet.
3971If we were to calculate ((A^4)):&lt;&#x2F;p&gt;
3972&lt;div class=&quot;matrix&quot;&gt;
39731 &#x27; 2 &#x27; 1 &#x27;   &#x27;   \\
3974  &#x27;   &#x27;   &#x27;   &#x27;   \\
3975  &#x27;   &#x27;   &#x27;   &#x27;   \\
3976  &#x27;   &#x27;   &#x27;   &#x27;   \\
3977  &#x27;   &#x27;   &#x27;   &#x27;
3978&lt;&#x2F;div&gt;
3979&lt;p&gt;We don&#x27;t need to calculate anymore. We now know that the graph is
3980strongly connected!&lt;&#x2F;p&gt;
3981&lt;hr &#x2F;&gt;
3982&lt;p&gt;Congratulations! You&#x27;ve completed this tiny introduction to graphs.
3983Now you can play around with them and design your own connections.&lt;&#x2F;p&gt;
3984&lt;p&gt;Hold the left mouse button on the above area and drag it down to create
3985a new node, or drag a node to this area to delete it.&lt;&#x2F;p&gt;
3986&lt;p&gt;To create new connections, hold the right mouse button on the node you
3987want to start with, and drag it to the node you want it to be connected to.&lt;&#x2F;p&gt;
3988&lt;p&gt;To delete the connections coming from a specific node, middle click it.&lt;&#x2F;p&gt;
3989&lt;table&gt;&lt;tr&gt;&lt;td style=&quot;width:100%;&quot;&gt;
3990  &lt;button onclick=&quot;resetConnections()&quot;&gt;Reset connections&lt;&#x2F;button&gt;
3991  &lt;button onclick=&quot;clearNodes()&quot;&gt;Clear all the nodes&lt;&#x2F;button&gt;
3992  &lt;br &#x2F;&gt;
3993  &lt;br &#x2F;&gt;
3994  &lt;label for=&quot;matrixOrder&quot;&gt;Show matrix of order:&lt;&#x2F;label&gt;
3995  &lt;input id=&quot;matrixOrder&quot; type=&quot;number&quot; min=&quot;1&quot; max=&quot;5&quot;
3996                          value=&quot;1&quot; oninput=&quot;updateOrder()&quot;&gt;
3997  &lt;br &#x2F;&gt;
3998  &lt;label for=&quot;matrixAccum&quot;&gt;Show accumulated matrix&lt;&#x2F;label&gt;
3999  &lt;input id=&quot;matrixAccum&quot; type=&quot;checkbox&quot; onchange=&quot;updateOrder()&quot;&gt;
4000  &lt;br &#x2F;&gt;
4001  &lt;br &#x2F;&gt;
4002  &lt;div&gt;
4003    &lt;table id=&quot;matrixTable&quot;&gt;&lt;&#x2F;table&gt;
4004  &lt;&#x2F;div&gt;
4005&lt;&#x2F;td&gt;&lt;td&gt;
4006  &lt;canvas id=&quot;canvas&quot; width=&quot;400&quot; height=&quot;400&quot; oncontextmenu=&quot;return false;&quot;&gt;
4007  Looks like your browser won&#x27;t let you see this fancy example :(
4008  &lt;&#x2F;canvas&gt;
4009  &lt;br &#x2F;&gt;
4010&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;table&gt;
4011&lt;script src=&quot;tinyparser.js&quot;&gt;&lt;&#x2F;script&gt;
4012&lt;script src=&quot;enhancements.js&quot;&gt;&lt;&#x2F;script&gt;
4013&lt;script src=&quot;graphs.js&quot;&gt;&lt;&#x2F;script&gt;
4014</content>
4015	</entry>
4016	<entry xml:lang="en">
4017		<title>Installing NixOS</title>
4018		<published>2017-05-13T00:00:00+00:00</published>
4019		<updated>2019-02-16T00:00:00+00:00</updated>
4020		<link href="https://lonami.dev/blog/installing-nixos/" type="text/html"/>
4021		<id>https://lonami.dev/blog/installing-nixos/</id>
4022		<content type="html">&lt;h2 id=&quot;update&quot;&gt;Update&lt;&#x2F;h2&gt;
4023&lt;p&gt;&lt;em&gt;Please see &lt;a href=&quot;..&#x2F;installing_nixos_2&#x2F;index.html&quot;&gt;my followup post with NixOS&lt;&#x2F;a&gt; for a far better experience with it&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
4024&lt;hr &#x2F;&gt;
4025&lt;p&gt;Today I decided to install &lt;a href=&quot;http:&#x2F;&#x2F;nixos.org&#x2F;&quot;&gt;NixOS&lt;&#x2F;a&gt; as a recommendation, a purely functional Linux distribution, since &lt;a href=&quot;https:&#x2F;&#x2F;xubuntu.org&#x2F;&quot;&gt;Xubuntu&lt;&#x2F;a&gt; kept crashing. Here&#x27;s my journey, and how I managed to install it from a terminal for the first time in my life. Steps aren&#x27;t hard, but they may not seem obvious at first.&lt;&#x2F;p&gt;
4026&lt;ul&gt;
4027&lt;li&gt;
4028&lt;p&gt;Grab the Live CD, burn it on a USB stick and boot. I recommend using &lt;a href=&quot;https:&#x2F;&#x2F;etcher.io&#x2F;&quot;&gt;Etcher&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
4029&lt;&#x2F;li&gt;
4030&lt;li&gt;
4031&lt;p&gt;Type &lt;code&gt;systemctl start display-manager&lt;&#x2F;code&gt; and wait.&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#1&quot;&gt;1&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;
4032&lt;&#x2F;li&gt;
4033&lt;li&gt;
4034&lt;p&gt;Open both the manual and the &lt;code&gt;konsole&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
4035&lt;&#x2F;li&gt;
4036&lt;li&gt;
4037&lt;p&gt;Connect to the network using the GUI.&lt;&#x2F;p&gt;
4038&lt;&#x2F;li&gt;
4039&lt;li&gt;
4040&lt;p&gt;Create the disk partitions by using &lt;code&gt;fdisk&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
4041&lt;p&gt;You can list them with &lt;code&gt;fdisk -l&lt;&#x2F;code&gt;, modify a certain drive with &lt;code&gt;fdisk &#x2F;dev&#x2F;sdX&lt;&#x2F;code&gt; (for instance, &lt;code&gt;&#x2F;dev&#x2F;sda&lt;&#x2F;code&gt;) and follow the instructions.&lt;&#x2F;p&gt;
4042&lt;p&gt;To create the file system, use &lt;code&gt;mkfs.ext4 -L &amp;lt;label&amp;gt; &#x2F;dev&#x2F;sdXY&lt;&#x2F;code&gt; and swap with &lt;code&gt;mkswap -L &amp;lt;label&amp;gt; &#x2F;dev&#x2F;sdXY&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
4043&lt;p&gt;The EFI partition should be done with &lt;code&gt;mkfs.vfat&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
4044&lt;&#x2F;li&gt;
4045&lt;li&gt;
4046&lt;p&gt;Mount the target to &lt;code&gt;&#x2F;mnt&lt;&#x2F;code&gt; e.g. if the label was &lt;code&gt;nixos&lt;&#x2F;code&gt;, &lt;code&gt;mount &#x2F;dev&#x2F;disk&#x2F;by-label&#x2F;nixos &#x2F;mnt&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
4047&lt;&#x2F;li&gt;
4048&lt;li&gt;
4049&lt;p&gt;&lt;code&gt;mkdir &#x2F;mnt&#x2F;boot&lt;&#x2F;code&gt; and then mount your EFI partition to it.&lt;&#x2F;p&gt;
4050&lt;&#x2F;li&gt;
4051&lt;li&gt;
4052&lt;p&gt;Generate a configuration template with &lt;code&gt;nixos-generate-config --root &#x2F;mnt&lt;&#x2F;code&gt;, and modify it with &lt;code&gt;nano &#x2F;etc&#x2F;nixos&#x2F;configuration.nix&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
4053&lt;&#x2F;li&gt;
4054&lt;li&gt;
4055&lt;p&gt;While modifying the configuration, make sure to add &lt;code&gt;boot.loader.grub.device = &amp;quot;&#x2F;dev&#x2F;sda&amp;quot;&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
4056&lt;&#x2F;li&gt;
4057&lt;li&gt;
4058&lt;p&gt;More useful configuration things are:&lt;&#x2F;p&gt;
4059&lt;ul&gt;
4060&lt;li&gt;Uncomment the whole &lt;code&gt;i18n&lt;&#x2F;code&gt; block.&lt;&#x2F;li&gt;
4061&lt;li&gt;Add some essential packages like &lt;code&gt;environment.systemPackages = with pkgs; [wget git firefox pulseaudio networkmanagerapplet];&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
4062&lt;li&gt;If you want to use XFCE, add &lt;code&gt;services.xserver.desktopManager.xfce.enable = true;&lt;&#x2F;code&gt;, otherwise, you don&#x27;t need &lt;code&gt;networkmanagerapplet&lt;&#x2F;code&gt; either. Make sure to add &lt;code&gt;networking.networkmanager.enable = true;&lt;&#x2F;code&gt; too.&lt;&#x2F;li&gt;
4063&lt;li&gt;Define some user for yourself (modify &lt;code&gt;guest&lt;&#x2F;code&gt; name) and use a UID greater than 1000. Also, add yourself to &lt;code&gt;extraGroups = [&amp;quot;wheel&amp;quot; &amp;quot;networkmanager&amp;quot;];&lt;&#x2F;code&gt; (the first to be able to &lt;code&gt;sudo&lt;&#x2F;code&gt;, the second to use network related things).&lt;&#x2F;li&gt;
4064&lt;&#x2F;ul&gt;
4065&lt;&#x2F;li&gt;
4066&lt;li&gt;
4067&lt;p&gt;Run &lt;code&gt;nixos-install&lt;&#x2F;code&gt;. If you ever modify that file again, to add more packages for instance (this is how they&#x27;re installed), run &lt;code&gt;nixos-rebuild switch&lt;&#x2F;code&gt; (or use &lt;code&gt;test&lt;&#x2F;code&gt; to test but don&#x27;t boot to it, or &lt;code&gt;boot&lt;&#x2F;code&gt; not to switch but to use on next boot.&lt;&#x2F;p&gt;
4068&lt;&#x2F;li&gt;
4069&lt;li&gt;
4070&lt;p&gt;&lt;code&gt;reboot&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
4071&lt;&#x2F;li&gt;
4072&lt;li&gt;
4073&lt;p&gt;Login as &lt;code&gt;root&lt;&#x2F;code&gt;, and set a password for your user with &lt;code&gt;passwd &amp;lt;user&amp;gt;&lt;&#x2F;code&gt;. Done!&lt;&#x2F;p&gt;
4074&lt;&#x2F;li&gt;
4075&lt;&#x2F;ul&gt;
4076&lt;p&gt;I enjoyed the process of installing it, and it&#x27;s really cool that it has versioning and is so clean to keep track of which packages you install. But not being able to run arbitrary binaries by default is something very limitting in my opinion, though they&#x27;ve done a good job.&lt;&#x2F;p&gt;
4077&lt;p&gt;I&#x27;m now back to Xubuntu, with a fresh install.&lt;&#x2F;p&gt;
4078&lt;h2 id=&quot;update-1&quot;&gt;Update&lt;&#x2F;h2&gt;
4079&lt;p&gt;It is not true that &amp;quot;they don&#x27;t allow running arbitrary binaries by default&amp;quot;, as pointed out in their &lt;a href=&quot;https:&#x2F;&#x2F;nixos.org&#x2F;nixpkgs&#x2F;manual&#x2F;#sec-fhs-environments&quot;&gt;manual, buildFHSUserEnv&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
4080&lt;blockquote&gt;
4081&lt;p&gt;&lt;code&gt;buildFHSUserEnv&lt;&#x2F;code&gt; provides a way to build and run FHS-compatible lightweight sandboxes. It creates an isolated root with bound &lt;code&gt;&#x2F;nix&#x2F;store&lt;&#x2F;code&gt;, so its footprint in terms of disk space needed is quite small. This allows one to run software which is hard or unfeasible to patch for NixOS -- 3rd-party source trees with FHS assumptions, games distributed as tarballs, software with integrity checking and&#x2F;or external self-updated binaries. It uses Linux namespaces feature to create temporary lightweight environments which are destroyed after all child processes exit, without root user rights requirement.&lt;&#x2F;p&gt;
4082&lt;&#x2F;blockquote&gt;
4083&lt;p&gt;Thanks to &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;bb010g&quot;&gt;@bb010g&lt;&#x2F;a&gt; for pointing this out.&lt;&#x2F;p&gt;
4084&lt;h2 id=&quot;notes&quot;&gt;Notes&lt;&#x2F;h2&gt;
4085&lt;div class=&quot;footnote-definition&quot; id=&quot;1&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;&#x2F;sup&gt;
4086&lt;p&gt;The keyboard mapping is a bit strange. On my Spanish keyboard, the keys were as follows:&lt;&#x2F;p&gt;
4087&lt;&#x2F;div&gt;
4088&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Keyboard&lt;&#x2F;th&gt;&lt;th&gt;Maps to&lt;&#x2F;th&gt;&lt;th&gt;Shift&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
4089&lt;tr&gt;&lt;td&gt;&#x27;&lt;&#x2F;td&gt;&lt;td&gt;-&lt;&#x2F;td&gt;&lt;td&gt;_&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
4090&lt;tr&gt;&lt;td&gt;´&lt;&#x2F;td&gt;&lt;td&gt;&#x27;&lt;&#x2F;td&gt;&lt;td&gt;&amp;quot;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
4091&lt;tr&gt;&lt;td&gt;`&lt;&#x2F;td&gt;&lt;td&gt;[&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
4092&lt;tr&gt;&lt;td&gt;+&lt;&#x2F;td&gt;&lt;td&gt;]&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
4093&lt;tr&gt;&lt;td&gt;¡&lt;&#x2F;td&gt;&lt;td&gt;=&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
4094&lt;tr&gt;&lt;td&gt;-&lt;&#x2F;td&gt;&lt;td&gt;&#x2F;&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
4095&lt;tr&gt;&lt;td&gt;ñ&lt;&#x2F;td&gt;&lt;td&gt;;&lt;&#x2F;td&gt;&lt;td&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
4096&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
4097</content>
4098	</entry>
4099</feed>