AFK Macro

Idle Mafia Macro/Source/src/Instances.cs

Instances.cs

Several Roblox windows · 67 lines · 2 KB

using System;
using System.Collections.Generic;
using System.Linq;
namespace IdleMafiaBot
{
sealed class Instance
{
public IntPtr Handle;
public string Name;
public Header Head;
public DateTime LookedAt;
public string Label { get { return Name ?? "Roblox (reading...)"; } }
}
static class Instances
{
public static List<Instance> Read(List<Instance> known, Settings s, string dir)
{
var list = new List<Instance>();
var now = DateTime.UtcNow;
foreach (var h in RobloxWindow.All())
{
var had = known == null ? null : known.FirstOrDefault(i => i.Handle == h);
if (had != null && (had.Name != null || now - had.LookedAt < TimeSpan.FromSeconds(30))) { list.Add(had); continue; }
var head = HeaderIn(h, s);
list.Add(new Instance { Handle = h, Head = head, Name = head == null || !Accounts.Plausible(head.Name) ? null : Accounts.Known(dir, Accounts.Tidy(head.Name.Trim())), LookedAt = now });
}
return list;
}
internal static Instance Pick(List<Instance> open, string account, bool pickedClosed)
{
var named = open.Where(i => i.Name != null).ToList();
return named.FirstOrDefault(i => Accounts.Same(i.Name, account)) ?? (pickedClosed ? named.FirstOrDefault() : null);
}
public static bool SameWindows(List<Instance> a, List<Instance> b)
{
if (a == null || b == null || a.Count != b.Count) return false;
for (int i = 0; i < a.Count; i++)
if (a[i].Handle != b[i].Handle || a[i].Name != b[i].Name) return false;
return true;
}
internal static Header HeaderIn(IntPtr h, Settings s)
{
try
{
var g = new Game(msg => { }, () => false);
g.Win.Pinned = h;
if (!g.Win.Find() || g.Win.Minimized) return null;
using (var f = g.Win.Capture())
{
var v = g.ViewOf(f);
if (v.Problem != null) return null;
g.SeedMax(s.BarMax);
return g.ReadHeader(f, v);
}
}
catch (Exception) { return null; }
}
}
}