Ejemplo n.º 1
0
void sampgdk_timer_process_timers(void *plugin) {
  long now;
  long elapsed;
  int i;
  struct sampgdk_timer *timer;

  assert(plugin != NULL);

  now = sampgdk_timer_clock();

  for (i = 0; i < timers.count; i++) {
    timer = sampgdk_array_get(&timers, i);

    if (!timer->is_set) {
      continue;
    }

    if (plugin != NULL && timer->plugin != plugin) {
      continue;
    }

    elapsed = now - timer->started;

    if (elapsed >= timer->interval) {
      fire_timer(i + 1, elapsed);
    }
  }
}
Ejemplo n.º 2
0
Archivo: timer.c Proyecto: Zeex/sampgdk
static void _sampgdk_timer_fire(int timerid, int64_t elapsed) {
  struct _sampgdk_timer_info *timer;
  int64_t now = _sampgdk_timer_now();
  int64_t started;

  assert(timerid > 0 && timerid <= _sampgdk_timers.count);
  timer = (struct _sampgdk_timer_info *)sampgdk_array_get(&_sampgdk_timers, timerid - 1);

  assert(timer->is_set);
  started = timer->started;

  sampgdk_log_debug("Firing timer %d, now = %" PRId64 ", elapsed = %" PRId64,
      timerid, now, elapsed);
  ((sampgdk_timer_callback)timer->callback)(timerid, timer->param);

  /* We don't want to kill the same timer twice, so make sure it's not
   * been killed inside the timer callback.
   */
  if (timer->is_set && timer->started == started) {
    if (timer->repeat) {
      timer->started = now - (elapsed - timer->interval);
    } else {
      sampgdk_timer_kill(timerid);
    }
  }
}
Ejemplo n.º 3
0
Archivo: timer.c Proyecto: Zeex/sampgdk
void sampgdk_timer_process_timers(void *plugin) {
  int64_t now;
  int64_t elapsed;
  int i;
  struct _sampgdk_timer_info *timer;

  assert(plugin != NULL);

  now = _sampgdk_timer_now();

  for (i = 0; i < _sampgdk_timers.count; i++) {
    timer = (struct _sampgdk_timer_info *)sampgdk_array_get(&_sampgdk_timers, i);

    if (!timer->is_set
        || (plugin != NULL && timer->plugin != plugin)) {
      continue;
    }

    elapsed = now - timer->started;

    if (elapsed >= timer->interval) {
      _sampgdk_timer_fire(i + 1, elapsed);
    }
  }
}
Ejemplo n.º 4
0
void sampgdk_fakeamx_get_string(cell address, char *dest, int size) {
  assert(is_cell_aligned(address));
  assert(dest != NULL);

  amx_GetString(dest, (cell *)sampgdk_array_get(&global.heap,
                                                address / sizeof(cell)),
                                                0, size);
}
Ejemplo n.º 5
0
bool sampgdk_callback_invoke(AMX *amx,
                             const char *name,
                             int paramcount,
                             cell *retval)
{
  struct _sampgdk_callback_info *callback;
  struct _sampgdk_callback_info *callback_filter;
  cell params[_SAMPGDK_CALLBACK_MAX_ARGS + 1];
  void **plugins;
  int num_plugins;
  int i;

  assert(amx != NULL);

  callback = _sampgdk_callback_find(name);
  callback_filter = sampgdk_array_get(&_sampgdk_callbacks, -1);

  assert(callback_filter != NULL);

  if (paramcount > _SAMPGDK_CALLBACK_MAX_ARGS) {
    sampgdk_log_error("Too many callback arguments (at most %d allowed)",
                      _SAMPGDK_CALLBACK_MAX_ARGS);
    return true;
  }

  params[0] = paramcount * sizeof(cell);
  memcpy(&params[1], sampgdk_param_get_start(amx), params[0]);

  plugins = sampgdk_plugin_get_plugins(&num_plugins);

  for (i = 0; i < num_plugins; i++) {
    void *func;

    func = sampgdk_plugin_get_symbol(plugins[i], callback_filter->name);
    if (func != NULL
        && !((_sampgdk_callback_filter)func)(amx, name, params, retval)) {
      continue;
    }

    if (callback == NULL || callback->handler == NULL) {
      continue;
    }

    func = sampgdk_plugin_get_symbol(plugins[i], callback->name);
    if (func != NULL
        && !((sampgdk_callback)callback->handler)(amx, func, retval)) {
      return false;
    }
  }

  return true;
}
Ejemplo n.º 6
0
int sampgdk_array_insert_ordered(struct sampgdk_array *a, void *elem,
                                 int (*comp)(const void *x, const void *y))
{
  int index;

  for (index = 0; index < a->count; index++) {
    if (comp(sampgdk_array_get(a, index), elem) >= 0) {
      return sampgdk_array_insert_single(a, index, elem);
    }
  }

  return sampgdk_array_append(a, elem);
}
Ejemplo n.º 7
0
void sampgdk_callback_unregister(const char *name) {
  const struct sampgdk_callback *ptr;
  int index;

  for (index = 0; index < callbacks.count; index++) {
    ptr = (const struct sampgdk_callback *)sampgdk_array_get(&callbacks,
                                                             index);
    if (strcmp(ptr->name, name) == 0) {
      sampgdk_array_remove_single(&callbacks, index);
      break;
    }
  }
}
Ejemplo n.º 8
0
bool sampgdk_callback_get(int index, char **name) {
  struct _sampgdk_callback_info *callback;

  assert(name != NULL);

  if (index < 0 || index >= _sampgdk_callbacks.count) {
    return false;
  }

  callback = sampgdk_array_get(&_sampgdk_callbacks, index);
  *name = callback->name;

  return true;
}
Ejemplo n.º 9
0
Archivo: timer.c Proyecto: Zeex/sampgdk
static int _sampgdk_timer_find_slot(void) {
  int i;

  for (i = 0; i < _sampgdk_timers.count; i++) {
    struct _sampgdk_timer_info *timer;

    timer = (struct _sampgdk_timer_info *)sampgdk_array_get(&_sampgdk_timers, i);
    if (!timer->is_set) {
      return i;
    }
  }

  return -1;
}
Ejemplo n.º 10
0
static int find_slot() {
  int i;

  for (i = 0; i < timers.count; i++) {
    struct sampgdk_timer *timer;

    timer = sampgdk_array_get(&timers, i);
    if (!timer->is_set) {
      return i;
    }
  }

  return -1;
}
Ejemplo n.º 11
0
int sampgdk_timer_kill(int timerid) {
  struct sampgdk_timer *timer;

  if (timerid <= 0 || timerid > timers.count) {
    return -EINVAL;
  }

  timer = sampgdk_array_get(&timers, timerid - 1);
  if (!timer->is_set) {
    return -EINVAL;
  }

  timer->is_set = false;
  return 0;
}
Ejemplo n.º 12
0
int sampgdk_callback_register(const char *name,
                              sampgdk_callback_handler handler) {
  int error;
  struct sampgdk_callback info;
  struct sampgdk_callback *ptr;
  int index;
 
  assert(name != NULL);
  assert(handler != NULL);

  /* This is rather an exception than a rule. */
  sampgdk_callback_init();

  ptr = sampgdk_callback_find(name);
  if (ptr != NULL) {
    ptr->handler = handler;
    return 0;
  }

  info.name = malloc(strlen(name) + 1);
  if (info.name == NULL) {
    return -ENOMEM;
  }

  strcpy(info.name, name);
  info.handler = handler;

  /* Maintain element order (by name). */
  for (index = 0; index < callbacks.count; index++) {
    ptr = (struct sampgdk_callback *)sampgdk_array_get(&callbacks, index);
    if (strcmp(ptr->name, name) >= 0) {
      error = sampgdk_array_insert_single(&callbacks, index, &info);
      break;
    }
  }

  /* Append to the end. */
  if (index == callbacks.count) {
    error = sampgdk_array_append(&callbacks, &info);
  }

  if (error < 0) {
    free(info.name);
    return error;
  }

  return 0;
}
Ejemplo n.º 13
0
Archivo: timer.c Proyecto: Zeex/sampgdk
int sampgdk_timer_kill(int timerid) {
  struct _sampgdk_timer_info *timer;

  if (timerid <= 0 || timerid > _sampgdk_timers.count) {
    return -EINVAL;
  }

  timer = (struct _sampgdk_timer_info *)sampgdk_array_get(&_sampgdk_timers, timerid - 1);
  if (!timer->is_set) {
    return -EINVAL;
  }

  timer->is_set = false;

  sampgdk_log_debug("Killed timer %d", timerid);

  return 0;
}
Ejemplo n.º 14
0
Archivo: array.c Proyecto: Zeex/sampgdk
int sampgdk_array_find(struct sampgdk_array *a,
                       const void *key,
                       sampgdk_array_cmp cmp) {
  int index;
  void *cur;

  assert(a != NULL);
  assert(cmp != NULL);

  for (index = 0; index < a->count; index++) {
    cur = sampgdk_array_get(a, index);
    if (cmp(key, cur) == 0) {
      return index;
    }
  }

  return -EINVAL;
}
Ejemplo n.º 15
0
int sampgdk_fakeamx_push_string(const char *src, int *size, cell *address) {
  int src_size;
  int error;

  assert(address != NULL);
  assert(src != NULL);

  src_size = (int)strlen(src) + 1;
  if ((error = sampgdk_fakeamx_push(src_size, address)) < 0) {
    return error;
  }

  amx_SetString((cell *)sampgdk_array_get(&global.heap,
                *address / sizeof(cell)), src, 0, 0, src_size);

  if (size != NULL) {
    *size = src_size;
  }

  return 0;
}
Ejemplo n.º 16
0
int sampgdk_callback_register(const char *name,
                              sampgdk_callback handler) {
  int error;
  int i;
  struct _sampgdk_callback_info callback;
  struct _sampgdk_callback_info *ptr;

  assert(name != NULL);

  ptr = _sampgdk_callback_find(name);
  if (ptr != NULL) {
    return sampgdk_array_get_index(&_sampgdk_callbacks, ptr);
  }

  callback.name = malloc(strlen(name) + 1);
  if (callback.name == NULL) {
    return -ENOMEM;
  }

  callback.handler = handler;
  strcpy(callback.name, name);

  /* Keep callbacks ordered by name.
   * This allows us to use binary search in sampgdk_callback_find().
   */
  for (i = 0; i < _sampgdk_callbacks.count - 1; i++) {
    ptr = sampgdk_array_get(&_sampgdk_callbacks, i);
    if (strcmp(name, ptr->name) <= 0) {
      break;
    }
  }

  error = sampgdk_array_insert(&_sampgdk_callbacks, i, 1, &callback);
  if (error < 0) {
    free(callback.name);
    return error;
  }

  return error; /* index */
}
Ejemplo n.º 17
0
int sampgdk_native_register(const char *name, AMX_NATIVE func) {
  AMX_NATIVE_INFO info;
  AMX_NATIVE_INFO *ptr;
  int index;

  info.name = name;
  info.func = func;

  assert(name != 0);

  /* Keep natives ordered by name.
   * This allows us to use binary search in sampgdk_native_find().
   */
  for (index = 0; index < _sampgdk_natives.count - 1; index++) {
    ptr = sampgdk_array_get(&_sampgdk_natives, index);
    if (strcmp(name, ptr->name) <= 0) {
      break;
    }
  }

  return sampgdk_array_insert_single(&_sampgdk_natives, index, &info);
}
Ejemplo n.º 18
0
static void fire_timer(int timerid, long elapsed) {
  struct sampgdk_timer *timer;

  assert(timerid > 0 && timerid <= timers.count);

  timer = sampgdk_array_get(&timers, timerid - 1);
  if (!timer->is_set) {
    return;
  }

  (timer->callback)(timerid, timer->param);

  /* At this point the could be killed by the timer callback,
   * so the timer pointer may be no longer valid.
   */
  if (timer->is_set) {
    if (timer->repeat) {
      timer->started = sampgdk_timer_clock() - (elapsed - timer->interval);
    } else {
      sampgdk_timer_kill(timerid);
    }
  }
}
Ejemplo n.º 19
0
void sampgdk_fakeamx_get_cell(cell address, cell *value) {
  assert(is_cell_aligned(address));
  assert(value != NULL);

  *value = *(cell *)sampgdk_array_get(&global.heap, address / sizeof(cell));
}